Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dataview-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-fast-compare": patch
---

Fix `isEqual` hanging forever when comparing two `DataView`s. They're now compared byte by byte.
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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).
Expand Down
8 changes: 8 additions & 0 deletions pack-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\") {",
Expand All @@ -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;",
Expand Down
62 changes: 60 additions & 2 deletions test/fixtures/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Loading