Skip to content

Instantly share code, notes, and snippets.

@famzah
Created September 22, 2021 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save famzah/ab6e9202fea64898ab36eb2daf1074cb to your computer and use it in GitHub Desktop.
Save famzah/ab6e9202fea64898ab36eb2daf1074cb to your computer and use it in GitHub Desktop.
Prove that we can compare ArrayBuffer variables by casting them to a Uint8Array TypedArray view
// in regards to: https://github.com/inspect-js/node-deep-equal/issues/94
/* global console */
/* eslint-disable no-console */
/* global ArrayBuffer */
/* global Uint8Array */
/* global Float32Array */
/* global Float64Array */
/* global Uint32Array */
import deepEqual from "deep-equal";
function dump_buffer_in_different_representations(buffer) {
console.dir(buffer);
console.dir(new Uint8Array(buffer));
console.dir(new Uint32Array(buffer));
console.dir(new Float32Array(buffer));
console.dir(new Float64Array(buffer));
}
function assign_single_value_to_ArrayBuffer(buffer) {
const bigview = new Uint32Array(buffer);
bigview[1] = 20000000000020; // this is a 4-bytes value at position 1 (starting is 0)
bigview[9999] = 20000000000020; // this "out of bounds" assignment is simply ignored by Node.js :)
}
const buffer1 = new ArrayBuffer(8); // initial value of 0's
const buffer2 = new ArrayBuffer(16); // initial value of 0's
const buffer3 = new ArrayBuffer(8); // initial value of 0's
console.log("** Listing the representations of the empty buffer:\n");
dump_buffer_in_different_representations(buffer1);
assign_single_value_to_ArrayBuffer(buffer1);
assign_single_value_to_ArrayBuffer(buffer2);
console.log("\n** Listing the representations of the buffer with data:\n");
dump_buffer_in_different_representations(buffer1);
console.log("\n** Dumping all ArrayBuffers:");
console.dir(buffer1);
console.dir(buffer2);
console.dir(buffer3);
console.log("\n** Are two ArrayBuffers with the same length but different data equal?");
console.log(
deepEqual(
new Uint8Array(buffer1),
new Uint8Array(buffer3) // all values are 0's
)
);
console.log("** Are two ArrayBuffers with the same data but different length equal?");
console.log(
deepEqual(
new Uint8Array(buffer1),
new Uint8Array(buffer2) // same value as "buffer1" but with double length
)
);
console.log("** Is the same ArrayBuffer equal?");
console.log(
deepEqual(
new Uint8Array(buffer1),
new Uint8Array(buffer1)
)
);
/* This outputs the following:
** Listing the representations of the empty buffer:
ArrayBuffer {
[Uint8Contents]: <00 00 00 00 00 00 00 00>,
byteLength: 8
}
Uint8Array(8) [
0, 0, 0, 0,
0, 0, 0, 0
]
Uint32Array(2) [ 0, 0 ]
Float32Array(2) [ 0, 0 ]
Float64Array(1) [ 0 ]
** Listing the representations of the buffer with data:
ArrayBuffer {
[Uint8Contents]: <00 00 00 00 14 40 e5 9c>,
byteLength: 8
}
Uint8Array(8) [
0, 0, 0, 0,
20, 64, 229, 156
]
Uint32Array(2) [ 0, 2632269844 ]
Float32Array(2) [ 0, -1.5170512629061701e-21 ]
Float64Array(1) [ -1.75961486096292e-169 ]
** Dumping all ArrayBuffers:
ArrayBuffer {
[Uint8Contents]: <00 00 00 00 14 40 e5 9c>,
byteLength: 8
}
ArrayBuffer {
[Uint8Contents]: <00 00 00 00 14 40 e5 9c 00 00 00 00 00 00 00 00>,
byteLength: 16
}
ArrayBuffer {
[Uint8Contents]: <00 00 00 00 00 00 00 00>,
byteLength: 8
}
** Are two ArrayBuffers with the same length but different data equal?
false
** Are two ArrayBuffers with the same data but different length equal?
false
** Is the same ArrayBuffer equal?
true
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment