| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a9a93f3 commit 7b72396
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ const bench = common.createBenchmark(main, { | |||
| 7 | 7 | len: [1e2, 1e3], | |
| 8 | 8 | strict: [0, 1], | |
| 9 | 9 | arrayBuffer: [0, 1], | |
| 10 | - method: ['deepEqual', 'notDeepEqual', 'unequal_length'], | ||
| 10 | + method: ['deepEqual', 'notDeepEqual', 'unequal_length', 'partial'], | ||
| 11 | 11 | }, { | |
| 12 | 12 | combinationFilter: (p) => { | |
| 13 | 13 | return p.strict === 1 || p.method === 'deepEqual'; | |
@@ -18,11 +18,16 @@ function main({ len, n, method, strict, arrayBuffer }) { | |||
| 18 | 18 | let actual = Buffer.alloc(len); | |
| 19 | 19 | let expected = Buffer.alloc(len + Number(method === 'unequal_length')); | |
| 20 | 20 | ||
| 21 | - | ||
| 22 | 21 | if (method === 'unequal_length') { | |
| 23 | 22 | method = 'notDeepEqual'; | |
| 24 | 23 | } | |
| 25 | 24 | ||
| 25 | + if (method === 'partial') { | ||
| 26 | + method = 'partialDeepStrictEqual'; | ||
| 27 | + } else if (strict) { | ||
| 28 | + method = method.replace('eep', 'eepStrict'); | ||
| 29 | + } | ||
| 30 | + | ||
| 26 | 31 | for (let i = 0; i < len; i++) { | |
| 27 | 32 | actual.writeInt8(i % 128, i); | |
| 28 | 33 | expected.writeInt8(i % 128, i); | |
@@ -33,10 +38,6 @@ function main({ len, n, method, strict, arrayBuffer }) { | |||
| 33 | 38 | expected[position] = expected[position] + 1; | |
| 34 | 39 | } | |
| 35 | 40 | ||
| 36 | - if (strict) { | ||
| 37 | - method = method.replace('eep', 'eepStrict'); | ||
| 38 | - } | ||
| 39 | - | ||
| 40 | 41 | const fn = assert[method]; | |
| 41 | 42 | ||
| 42 | 43 | if (arrayBuffer) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,12 +4,13 @@ const common = require('../common.js'); | |||
| 4 | 4 | const assert = require('assert'); | |
| 5 | 5 | ||
| 6 | 6 | const bench = common.createBenchmark(main, { | |
| 7 | - n: [25], | ||
| 7 | + n: [125], | ||
| 8 | 8 | size: [500], | |
| 9 | - extraProps: [0], | ||
| 9 | + extraProps: [0, 1], | ||
| 10 | 10 | datasetName: [ | |
| 11 | 11 | 'objects', | |
| 12 | 12 | 'sets', | |
| 13 | + 'setsWithObjects', | ||
| 13 | 14 | 'maps', | |
| 14 | 15 | 'circularRefs', | |
| 15 | 16 | 'typedArrays', | |
@@ -31,17 +32,29 @@ function createObjects(length, extraProps, depth = 0) { | |||
| 31 | 32 | foo: 'yarp', | |
| 32 | 33 | nope: { | |
| 33 | 34 | bar: '123', | |
| 34 | - ...extraProps ? { a: [1, 2, i] } : {}, | ||
| 35 | + ...(extraProps ? { a: [1, 2, i] } : {}), | ||
| 35 | 36 | c: {}, | |
| 36 | 37 | b: !depth ? createObjects(2, extraProps, depth + 1) : [], | |
| 37 | 38 | }, | |
| 38 | 39 | })); | |
| 39 | 40 | } | |
| 40 | 41 | ||
| 42 | + function createSetsWithObjects(length, extraProps, depth = 0) { | ||
| 43 | + return Array.from({ length }, (_, i) => new Set([ | ||
| 44 | + ...(extraProps ? [{}] : []), | ||
| 45 | + { | ||
| 46 | + simple: 'object', | ||
| 47 | + number: i, | ||
| 48 | + }, | ||
| 49 | + ['array', 'with', 'values'], | ||
| 50 | + new Set([[], {}, { nested: i }]), | ||
| 51 | + ])); | ||
| 52 | + } | ||
| 53 | + | ||
| 41 | 54 | function createSets(length, extraProps, depth = 0) { | |
| 42 | 55 | return Array.from({ length }, (_, i) => new Set([ | |
| 43 | 56 | 'yarp', | |
| 44 | - ...extraProps ? ['123', 1, 2] : [], | ||
| 57 | + ...(extraProps ? ['123', 1, 2] : []), | ||
| 45 | 58 | i + 3, | |
| 46 | 59 | null, | |
| 47 | 60 | { | |
@@ -56,7 +69,7 @@ function createSets(length, extraProps, depth = 0) { | |||
| 56 | 69 | ||
| 57 | 70 | function createMaps(length, extraProps, depth = 0) { | |
| 58 | 71 | return Array.from({ length }, (_, i) => new Map([ | |
| 59 | - ...extraProps ? [['primitiveKey', 'primitiveValue']] : [], | ||
| 72 | + ...(extraProps ? [['primitiveKey', 'primitiveValue']] : []), | ||
| 60 | 73 | [42, 'numberKey'], | |
| 61 | 74 | ['objectValue', { a: 1, b: i }], | |
| 62 | 75 | ['arrayValue', [1, 2, i]], | |
@@ -114,16 +127,23 @@ function createTypedArrays(length, extraParts) { | |||
| 114 | 127 | } | |
| 115 | 128 | ||
| 116 | 129 | function createArrayBuffers(length, extra) { | |
| 117 | - return Array.from({ length }, (_, n) => new ArrayBuffer(n + extra ? 1 : 0)); | ||
| 130 | + return Array.from({ length }, (_, n) => { | ||
| 131 | + const buffer = Buffer.alloc(n + (extra ? 1 : 0)); | ||
| 132 | + for (let i = 0; i < n; i++) { | ||
| 133 | + buffer.writeInt8(i % 128, i); | ||
| 134 | + } | ||
| 135 | + return buffer.buffer; | ||
| 136 | + }); | ||
| 118 | 137 | } | |
| 119 | 138 | ||
| 120 | 139 | function createDataViewArrayBuffers(length, extra) { | |
| 121 | - return Array.from({ length }, (_, n) => new DataView(new ArrayBuffer(n + extra ? 1 : 0))); | ||
| 140 | + return createArrayBuffers(length, extra).map((buffer) => new DataView(buffer)); | ||
| 122 | 141 | } | |
| 123 | 142 | ||
| 124 | 143 | const datasetMappings = { | |
| 125 | 144 | objects: createObjects, | |
| 126 | 145 | sets: createSets, | |
| 146 | + setsWithObjects: createSetsWithObjects, | ||
| 127 | 147 | maps: createMaps, | |
| 128 | 148 | circularRefs: createCircularRefs, | |
| 129 | 149 | typedArrays: createTypedArrays, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -243,7 +243,7 @@ function innerDeepEqual(val1, val2, mode, memos) { | |||
| 243 | 243 | TypedArrayPrototypeGetSymbolToStringTag(val2)) { | |
| 244 | 244 | return false; | |
| 245 | 245 | } | |
| 246 | - if (mode === kPartial) { | ||
| 246 | + if (mode === kPartial && val1.byteLength !== val2.byteLength) { | ||
| 247 | 247 | if (!isPartialArrayBufferView(val1, val2)) { | |
| 248 | 248 | return false; | |
| 249 | 249 | } | |
@@ -280,7 +280,7 @@ function innerDeepEqual(val1, val2, mode, memos) { | |||
| 280 | 280 | if (!isAnyArrayBuffer(val2)) { | |
| 281 | 281 | return false; | |
| 282 | 282 | } | |
| 283 | - if (mode !== kPartial) { | ||
| 283 | + if (mode !== kPartial || val1.byteLength === val2.byteLength) { | ||
| 284 | 284 | if (!areEqualArrayBuffers(val1, val2)) { | |
| 285 | 285 | return false; | |
| 286 | 286 | } | |
@@ -546,18 +546,18 @@ function partialObjectSetEquiv(a, b, mode, set, memo) { | |||
| 546 | 546 | } | |
| 547 | 547 | ||
| 548 | 548 | function setObjectEquiv(a, b, mode, set, memo) { | |
| 549 | - if (mode === kPartial) { | ||
| 550 | - return partialObjectSetEquiv(a, b, mode, set, memo); | ||
| 551 | - } | ||
| 552 | 549 | // Fast path for objects only | |
| 553 | - if (mode === kStrict && set.size === a.size) { | ||
| 550 | + if (mode !== kLoose && set.size === a.size) { | ||
| 554 | 551 | for (const val of a) { | |
| 555 | 552 | if (!setHasEqualElement(set, val, mode, memo)) { | |
| 556 | 553 | return false; | |
| 557 | 554 | } | |
| 558 | 555 | } | |
| 559 | 556 | return true; | |
| 560 | 557 | } | |
| 558 | + if (mode === kPartial) { | ||
| 559 | + return partialObjectSetEquiv(a, b, mode, set, memo); | ||
| 560 | + } | ||
| 561 | 561 | ||
| 562 | 562 | for (const val of a) { | |
| 563 | 563 | // Primitive values have already been handled above. | |
@@ -639,18 +639,18 @@ function partialObjectMapEquiv(a, b, mode, set, memo) { | |||
| 639 | 639 | } | |
| 640 | 640 | ||
| 641 | 641 | function mapObjectEquivalence(a, b, mode, set, memo) { | |
| 642 | - if (mode === kPartial) { | ||
| 643 | - return partialObjectMapEquiv(a, b, mode, set, memo); | ||
| 644 | - } | ||
| 645 | 642 | // Fast path for objects only | |
| 646 | - if (mode === kStrict && set.size === a.size) { | ||
| 643 | + if (mode !== kLoose && set.size === a.size) { | ||
| 647 | 644 | for (const { 0: key1, 1: item1 } of a) { | |
| 648 | 645 | if (!mapHasEqualEntry(set, b, key1, item1, mode, memo)) { | |
| 649 | 646 | return false; | |
| 650 | 647 | } | |
| 651 | 648 | } | |
| 652 | 649 | return true; | |
| 653 | 650 | } | |
| 651 | + if (mode === kPartial) { | ||
| 652 | + return partialObjectMapEquiv(a, b, mode, set, memo); | ||
| 653 | + } | ||
| 654 | 654 | for (const { 0: key1, 1: item1 } of a) { | |
| 655 | 655 | if (typeof key1 === 'object' && key1 !== null) { | |
| 656 | 656 | if (!mapHasEqualEntry(set, b, key1, item1, mode, memo)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -280,6 +280,11 @@ describe('Object Comparison Tests', () => { | |||
| 280 | 280 | [{ a: 1 }, 'value1'], | |
| 281 | 281 | ]), | |
| 282 | 282 | }, | |
| 283 | + { | ||
| 284 | + description: 'throws for Maps with mixed unequal entries', | ||
| 285 | + actual: new Map([[{ a: 2 }, 1], [1, 1], [{ b: 1 }, 1], [[], 1], [2, 1], [{ a: 1 }, 1]]), | ||
| 286 | + expected: new Map([[{ a: 1 }, 1], [[], 1], [2, 1], [{ a: 1 }, 1]]), | ||
| 287 | + }, | ||
| 283 | 288 | { | |
| 284 | 289 | description: 'throws for sets with different object values', | |
| 285 | 290 | actual: new Set([ | |
@@ -494,6 +499,11 @@ describe('Object Comparison Tests', () => { | |||
| 494 | 499 | actual: new Float32Array([+0.0]), | |
| 495 | 500 | expected: new Float32Array([-0.0]), | |
| 496 | 501 | }, | |
| 502 | + { | ||
| 503 | + description: 'throws when comparing two Uint8Array objects with non-matching entries', | ||
| 504 | + actual: { typedArray: new Uint8Array([1, 2, 3, 4, 5]) }, | ||
| 505 | + expected: { typedArray: new Uint8Array([1, 333, 2, 4]) }, | ||
| 506 | + }, | ||
| 497 | 507 | { | |
| 498 | 508 | description: 'throws when comparing two different urls', | |
| 499 | 509 | actual: new URL('http://foo'), | |
@@ -713,7 +723,7 @@ describe('Object Comparison Tests', () => { | |||
| 713 | 723 | { | |
| 714 | 724 | description: 'compares two Uint8Array objects', | |
| 715 | 725 | actual: { typedArray: new Uint8Array([1, 2, 3, 4, 5]) }, | |
| 716 | - expected: { typedArray: new Uint8Array([1, 2, 3]) }, | ||
| 726 | + expected: { typedArray: new Uint8Array([1, 2, 3, 5]) }, | ||
| 717 | 727 | }, | |
| 718 | 728 | { | |
| 719 | 729 | description: 'compares two Int16Array objects', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments