最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Compare equality of two Uint8Array - Stack Overflow

programmeradmin4浏览0评论

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
  • Why don't you just install buffer and use it as in Node.js – Teneff Commented Apr 28, 2023 at 7:42
  • Or check it's .prototype.compare implementation here – Teneff Commented Apr 28, 2023 at 7:45
  • Thank @Teneff for the suggestion, I prefer using native APIs over external libraries to maintain compatibility in my libraries. – Filip Seman Commented Apr 28, 2023 at 7:47
  • 1 I don't think so. This is basically a dupe of this question and as you can see, all answers are essentially the same: check length and use every. – tenshi Commented Apr 28, 2023 at 14:20
  • 1 I understand that it's essentially an array because it extends Iterator, but I was hoping for a more expressive method like the Buffer.compare API. – Filip Seman Commented Apr 28, 2023 at 15:23
 |  Show 1 more comment

4 Answers 4

Reset to default 13 +50

You'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;
}
发布评论

评论列表(0)

  1. 暂无评论