24 lines
955 B
JavaScript

/**
* Compares two ArrayBuffer objects byte by byte to check for equality.
*
* @param {ArrayBufferLike | Int32Array | Uint32Array | Int16Array | Uint16Array | Int8Array | Uint8Array | Uint8ClampedArray} buffer1 - The first ArrayBuffer to compare.
* @param {ArrayBufferLike | Int32Array | Uint32Array | Int16Array | Uint16Array | Int8Array | Uint8Array | Uint8ClampedArray} buffer2 - The second ArrayBuffer to compare.
* @returns {boolean} Returns true if the ArrayBuffer objects are equal byte by byte, otherwise false.
*/
export function areArrayBuffersEqual(buffer1, buffer2) {
if (buffer1.byteLength !== buffer2.byteLength) {
return false;
}
const view1 = new DataView(buffer1.buffer ?? buffer1);
const view2 = new DataView(buffer2.buffer ?? buffer2);
for (let i = 0; i < buffer1.byteLength; i++) {
if (view1.getUint8(i) !== view2.getUint8(i)) {
return false;
}
}
return true;
}