I am comparing two Uint8Array
using CRC32 to ensure the accuracy of the data being decompressed. However, I am facing the issue of not having an API like Uint8Array.equal()
to compare the arrays. Although there is Bufferpare()
available in Node.js, it is not supported in the browser, which I am also working on.
I have created a basic implementation, but I am unsure if there is a more straightforward approach or if I have overlooked any built-in comparison APIs.
function isEqual(arr1: Uint8Array, arr2: Uint8Array): boolean {
if (arr1.length !== arr2.length) {
return false
}
return arr1.every((value, index) => value === arr2[index])
}
I am comparing two Uint8Array
using CRC32 to ensure the accuracy of the data being decompressed. However, I am facing the issue of not having an API like Uint8Array.equal()
to compare the arrays. Although there is Buffer.compare()
available in Node.js, it is not supported in the browser, which I am also working on.
I have created a basic implementation, but I am unsure if there is a more straightforward approach or if I have overlooked any built-in comparison APIs.
function isEqual(arr1: Uint8Array, arr2: Uint8Array): boolean {
if (arr1.length !== arr2.length) {
return false
}
return arr1.every((value, index) => value === arr2[index])
}
Share
Improve this question
asked Apr 28, 2023 at 7:31
Filip SemanFilip Seman
1,7142 gold badges20 silver badges29 bronze badges
6
|
Show 1 more comment
4 Answers
Reset to default 13 +50You're not missing anything, there isn't currently an equality checking method for typed arrays (or regular arrays), or anything that can be succinctly used like one while remaining efficient with large arrays.
There is a proposal out there, but it doesn't seem to be getting much traction. One of the more contentious issues is that there shouldn't be a dedicated method just for typed arrays, and there should just be a static object equality checking method.
There isn't a standard object equality checking method yet, though proposals have been made in the past. Here's one that was withdrawn. You could of course create your own function like this, probably with a special conditions for ArrayBuffer.isView
to compare the length
first, before each property. There are many potential edge cases with a function like this, which is perhaps why a generic standard solution remains elusive.
For now the best way to compare 2 typed arrays is to compare the length
and loop over the values in your own function. You can do the second part with a method like every
or a simple for/while loop (a simple loop is likely faster as it will have less overhead).
There is indeed a way to compare two Uint8Arrays that has been built in to the vast majority of browsers for some time:
function areBytewiseEqual(a, b) {
return indexedDB.cmp(a, b) === 0;
}
This will also work if a
and/or b
are ArrayBuffers or DataViews.
(It's a weird API to have to get involved with just for this feature, and it's not what this method was directly intended for — but as far as I can tell from reading the spec, there's no logical reason not to use it this way.)
Here's a naive implementation in TypeScript:
export function isEqualBytes(
bytes1: Uint8Array,
bytes2: Uint8Array
): boolean {
if (bytes1.length !== bytes2.length) {
return false;
}
for (let i = 0; i < bytes1.length; i++) {
if (bytes1[i] !== bytes2[i]) {
return false;
}
}
return true;
}
In pure JS, just compare the values of the arrays:
const areUnit8ArraysEqual = (a, b) => {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
.prototype.compare
implementation here – Teneff Commented Apr 28, 2023 at 7:45every
. – tenshi Commented Apr 28, 2023 at 14:20Buffer.compare
API. – Filip Seman Commented Apr 28, 2023 at 15:23