From 758e457908d83ec982b0ed379ef37b0c873d39f6 Mon Sep 17 00:00:00 2001 From: streamich Date: Wed, 27 Nov 2019 21:23:48 +0100 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=F0=9F=90=9B=20improve=20TypeArray?= =?UTF-8?q?=20checks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (1) run test suite with revered arguments, too; (2) check of object "constructor" prop; (3) simplify TypeArray check --- spec/es6tests.js | 2 +- spec/index.spec.js | 3 +++ src/index.jst | 13 +------------ 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/spec/es6tests.js b/spec/es6tests.js index 85e7383..46b1596 100644 --- a/spec/es6tests.js +++ b/spec/es6tests.js @@ -287,7 +287,7 @@ module.exports = [ }, { description: 'pseudo array and equivalent typed array are not equal', - value1: {'0': 1, '1': 2, length: 2}, + value1: {'0': 1, '1': 2, length: 2, constructor: Int32Array}, value2: new Int32Array([1, 2]), equal: false } diff --git a/spec/index.spec.js b/spec/index.spec.js index dd08ab4..092990a 100644 --- a/spec/index.spec.js +++ b/spec/index.spec.js @@ -16,6 +16,9 @@ function testCases(equalFunc, suiteName, suiteTests) { (test.skip ? it.skip : it)(test.description, function() { assert.strictEqual(equalFunc(test.value1, test.value2), test.equal); }); + (test.skip ? it.skip : it)(test.description + ' (reverse arguments)', function() { + assert.strictEqual(equalFunc(test.value2, test.value1), test.equal); + }); }); }); }); diff --git a/src/index.jst b/src/index.jst index 317a89c..6a29826 100644 --- a/src/index.jst +++ b/src/index.jst @@ -41,18 +41,7 @@ module.exports = function equal(a, b) { return true; } - if (a.constructor.BYTES_PER_ELEMENT && ( - a instanceof Int8Array || - a instanceof Uint8Array || - a instanceof Uint8ClampedArray || - a instanceof Int16Array || - a instanceof Uint16Array || - a instanceof Int32Array || - a instanceof Uint32Array || - a instanceof Float32Array || - a instanceof Float64Array || - (envHasBigInt64Array && (a instanceof BigInt64Array || a instanceof BigUint64Array)) - )) { + if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) { length = a.length; if (length != b.length) return false; for (i = length; i-- !== 0;) From aca6575f320d037b9360c977127742d9af042c60 Mon Sep 17 00:00:00 2001 From: streamich Date: Wed, 27 Nov 2019 21:39:22 +0100 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Set=20is=20not=20equa?= =?UTF-8?q?l=20to=20pseudo-Set?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/es6tests.js | 25 +++++++++++++++++++++++-- src/index.jst | 13 +++++-------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/spec/es6tests.js b/spec/es6tests.js index 46b1596..8ac945c 100644 --- a/spec/es6tests.js +++ b/spec/es6tests.js @@ -138,7 +138,18 @@ module.exports = [ value1: undefined, value2: new Map, equal: false - } + }, + { + description: 'map and a pseudo map are not equal', + value1: map({}), + value2: { + constructor: Map, + size: 0, + has: () => true, + get: () => 1, + }, + equal: false + }, ] }, @@ -228,7 +239,17 @@ module.exports = [ value1: set([undefined]), value2: set([]), equal: false - } + }, + { + description: 'set and pseudo set are not equal', + value1: new Set, + value2: { + constructor: Set, + size: 0, + has: () => true, + }, + equal: false + }, ] }, diff --git a/src/index.jst b/src/index.jst index 6a29826..eabcf74 100644 --- a/src/index.jst +++ b/src/index.jst @@ -22,22 +22,19 @@ module.exports = function equal(a, b) { } {{? it.es6 }} - if (a instanceof Map) { + if ((a instanceof Map) && (b instanceof Map)) { if (a.size !== b.size) return false; - for(i of a.entries()) + for (i of a.entries()) if (!b.has(i[0])) return false; - - for(i of a.entries()) + for (i of a.entries()) if (!equal(i[1], b.get(i[0]))) return false; - return true; } - if (a instanceof Set) { + if ((a instanceof Set) && (b instanceof Set)) { if (a.size !== b.size) return false; - for(i of a.entries()) + for (i of a.entries()) if (!b.has(i[0])) return false; - return true; } From 16da7a40cf32b087f08471711fa62f686890c772 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 30 Nov 2019 09:48:22 +0000 Subject: [PATCH 3/3] docs: update benchmarks after the change --- README.md | 24 ++++++++++++------------ benchmark/package.json | 2 +- package.json | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 545357b..6dace06 100644 --- a/README.md +++ b/README.md @@ -59,18 +59,18 @@ console.log(equal(Int16Array([1, 2]), Int16Array([1, 2]))); // true Node.js v12.6.0: ``` -fast-deep-equal x 263,304 ops/sec ±0.57% (86 runs sampled) -fast-deep-equal/es6 x 210,257 ops/sec ±0.34% (89 runs sampled) -fast-equals x 233,740 ops/sec ±0.40% (91 runs sampled) -nano-equal x 187,624 ops/sec ±0.30% (93 runs sampled) -shallow-equal-fuzzy x 139,305 ops/sec ±0.37% (91 runs sampled) -underscore.isEqual x 72,636 ops/sec ±0.26% (89 runs sampled) -lodash.isEqual x 37,684 ops/sec ±1.14% (91 runs sampled) -deep-equal x 2,390 ops/sec ±0.36% (88 runs sampled) -deep-eql x 36,353 ops/sec ±0.55% (90 runs sampled) -ramda.equals x 12,169 ops/sec ±0.39% (92 runs sampled) -util.isDeepStrictEqual x 46,720 ops/sec ±0.38% (92 runs sampled) -assert.deepStrictEqual x 461 ops/sec ±0.72% (86 runs sampled) +fast-deep-equal x 261,950 ops/sec ±0.52% (89 runs sampled) +fast-deep-equal/es6 x 212,991 ops/sec ±0.34% (92 runs sampled) +fast-equals x 230,957 ops/sec ±0.83% (85 runs sampled) +nano-equal x 187,995 ops/sec ±0.53% (88 runs sampled) +shallow-equal-fuzzy x 138,302 ops/sec ±0.49% (90 runs sampled) +underscore.isEqual x 74,423 ops/sec ±0.38% (89 runs sampled) +lodash.isEqual x 36,637 ops/sec ±0.72% (90 runs sampled) +deep-equal x 2,310 ops/sec ±0.37% (90 runs sampled) +deep-eql x 35,312 ops/sec ±0.67% (91 runs sampled) +ramda.equals x 12,054 ops/sec ±0.40% (91 runs sampled) +util.isDeepStrictEqual x 46,440 ops/sec ±0.43% (90 runs sampled) +assert.deepStrictEqual x 456 ops/sec ±0.71% (88 runs sampled) The fastest is fast-deep-equal ``` diff --git a/benchmark/package.json b/benchmark/package.json index 939349c..9089fed 100644 --- a/benchmark/package.json +++ b/benchmark/package.json @@ -11,4 +11,4 @@ "shallow-equal-fuzzy": "latest", "underscore": "latest" } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 4287520..e0e8c45 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Fast deep equal", "main": "index.js", "scripts": { - "eslint": "eslint *.js benchmark spec", + "eslint": "eslint *.js benchmark/*.js spec/*.js", "build": "node build", "benchmark": "npm i && npm run build && cd ./benchmark && npm i && node ./", "test-spec": "mocha spec/*.spec.js -R spec",