MCPcopy Create free account
hub / github.com/denoland/std / equals32Bit

Function equals32Bit

bytes/equals.ts:28–52  ·  view source on GitHub ↗

Check whether byte slices are equal to each other using 32-bit comparisons. * * @param a First array to check equality. * @param b Second array to check equality. * @returns `true` if the arrays are equal, `false` otherwise. * * @private

(a: Uint8Array, b: Uint8Array)

Source from the content-addressed store, hash-verified

26 * @private
27 */
28function equals32Bit(a: Uint8Array, b: Uint8Array): boolean {
29 const len = a.length;
30 const compactOffset = 3 - ((a.byteOffset + 3) % 4);
31 const compactLen = Math.floor((len - compactOffset) / 4);
32 const compactA = new Uint32Array(
33 a.buffer,
34 a.byteOffset + compactOffset,
35 compactLen,
36 );
37 const compactB = new Uint32Array(
38 b.buffer,
39 b.byteOffset + compactOffset,
40 compactLen,
41 );
42 for (let i = 0; i < compactOffset; i++) {
43 if (a[i] !== b[i]) return false;
44 }
45 for (let i = 0; i < compactA.length; i++) {
46 if (compactA[i] !== compactB[i]) return false;
47 }
48 for (let i = compactOffset + compactLen * 4; i < len; i++) {
49 if (a[i] !== b[i]) return false;
50 }
51 return true;
52}
53
54/**
55 * Byte length threshold for when to use 32-bit comparisons, based on

Callers 1

equalsFunction · 0.85

Calls 1

floorMethod · 0.80

Tested by

no test coverage detected