diff --git a/.changeset/dataview-hang.md b/.changeset/dataview-hang.md new file mode 100644 index 0000000..0817344 --- /dev/null +++ b/.changeset/dataview-hang.md @@ -0,0 +1,5 @@ +--- +"react-fast-compare": patch +--- + +Fix `isEqual` hanging forever when comparing two `DataView`s. They're now compared byte by byte. diff --git a/index.js b/index.js index dd4af25..7aee9f6 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,10 @@ -/* global Map:readonly, Set:readonly, ArrayBuffer:readonly */ +/* global Map:readonly, Set:readonly, ArrayBuffer:readonly, DataView:readonly */ var hasElementType = typeof Element !== 'undefined'; var hasMap = typeof Map === 'function'; var hasSet = typeof Set === 'function'; var hasArrayBuffer = typeof ArrayBuffer === 'function' && !!ArrayBuffer.isView; +var hasDataView = typeof DataView === 'function'; // Note: We **don't** need `envHasBigInt64Array` in fde es6/index.js @@ -66,6 +67,19 @@ function equal(a, b) { } // END: Modifications + // START: Modifications: + // `DataView` passes `ArrayBuffer.isView()` below but has no `length` or + // indexes, so the typed-array loop would never end. Compare its bytes. + // See: https://github.com/FormidableLabs/react-fast-compare/issues/150 + if (hasDataView && a instanceof DataView && b instanceof DataView) { + length = a.byteLength; + if (length !== b.byteLength) return false; + for (i = length; i-- !== 0;) + if (a.getUint8(i) !== b.getUint8(i)) return false; + return true; + } + // END: Modifications + // Types: the casts stop `isView()` narrowing `a` and `b` to // `ArrayBufferView`, which has no `length` or index signature (typed // arrays have both, a `DataView` has neither). diff --git a/pack-snapshot.json b/pack-snapshot.json index 604e1bd..902029c 100644 --- a/pack-snapshot.json +++ b/pack-snapshot.json @@ -37,6 +37,8 @@ "", "var hasArrayBuffer = typeof ArrayBuffer === \"function\" && !!ArrayBuffer.isView;", "", + "var hasDataView = typeof DataView === \"function\";", + "", "function equal(a, b) {", " if (a === b) return true;", " if (a && b && typeof a == \"object\" && typeof b == \"object\") {", @@ -63,6 +65,12 @@ " while (!(i = it.next()).done) if (!b.has(i.value[0])) return false;", " return true;", " }", + " if (hasDataView && a instanceof DataView && b instanceof DataView) {", + " length = a.byteLength;", + " if (length !== b.byteLength) return false;", + " for (i = length; i-- !== 0; ) if (a.getUint8(i) !== b.getUint8(i)) return false;", + " return true;", + " }", " if (hasArrayBuffer && ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {", " length = a.length;", " if (length != b.length) return false;", diff --git a/test/fixtures/index.mjs b/test/fixtures/index.mjs index ddc4fa4..2e53a94 100644 --- a/test/fixtures/index.mjs +++ b/test/fixtures/index.mjs @@ -110,5 +110,63 @@ const custom = [ }, ]; -export { generic, es6, react, custom }; -export const all = [].concat(generic, es6, react, custom); +// A `DataView` over the given bytes. Comparing it through the typed-array loop +// (#150) reads `view[NaN]` forever. The trap turns that hang into a throw, so a +// regression fails the tests instead of stalling them (a synchronous loop can't +// be timed out), and `pnpm bench` skips versions that still have the bug. +const dataView = (bytes) => { + const view = new DataView(new Uint8Array(bytes).buffer); + Object.defineProperty(view, 'NaN', { + get() { + throw new Error('DataView compared as a typed array (#150)'); + }, + }); + return view; +}; + +const dataViews = [ + { + description: 'DataView', + tests: [ + { + description: 'empty DataViews are equal', + value1: dataView([]), + value2: dataView([]), + equal: true, + }, + { + description: 'DataViews with the same bytes are equal', + value1: dataView([1, 2, 3, 4]), + value2: dataView([1, 2, 3, 4]), + equal: true, + }, + { + description: 'DataViews with different bytes are not equal', + value1: dataView([1, 2, 3, 4]), + value2: dataView([1, 2, 3, 5]), + equal: false, + }, + { + description: 'DataViews with different lengths are not equal', + value1: dataView([1, 2, 3, 4]), + value2: dataView([1, 2, 3]), + equal: false, + }, + { + description: 'objects with equal DataViews are equal', + value1: { a: 1, view: dataView([1, 2, 3, 4]) }, + value2: { a: 1, view: dataView([1, 2, 3, 4]) }, + equal: true, + }, + { + description: 'objects with different DataViews are not equal', + value1: { a: 1, view: dataView([1, 2, 3, 4]) }, + value2: { a: 1, view: dataView([4, 3, 2, 1]) }, + equal: false, + }, + ], + }, +]; + +export { generic, es6, react, custom, dataViews }; +export const all = [].concat(generic, es6, react, custom, dataViews);