Skip to content

Instantly share code, notes, and snippets.

@malash
Last active December 24, 2023 20:18
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 malash/d5a1815a409964af88fd74085c7ba75e to your computer and use it in GitHub Desktop.
Save malash/d5a1815a409964af88fd74085c7ba75e to your computer and use it in GitHub Desktop.
NodeJS Buffer vs DataView performance benchmark
const os = require("os");
const benchmark = require("nodemark");
const str = "a".repeat(1 << 10);
const buffer = Buffer.from(str);
const dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.length);
console.log("NodeJS version:", process.version);
console.log("CPU name:", os.cpus()[0].model);
console.log(
"buffer[i]",
benchmark(() => {
for (let i = 0; i < buffer.length; i++) {
buffer[i] = 98;
}
})
);
console.log(
"dataView.setUint8",
benchmark(() => {
for (let i = 0; i < dataView.byteLength; i++) {
dataView.setUint8(i, 99);
}
})
);
console.log(
"dataView.setUint16",
benchmark(() => {
const d16 = (100 << 8) + 100;
for (let i = 0; i < dataView.byteLength; i += 2) {
dataView.setUint16(i, d16);
}
})
);
console.log(
"dataView.setUint32",
benchmark(() => {
const d32 = (101 << 24) + (101 << 16) + (101 << 8) + 101;
for (let i = 0; i < dataView.byteLength; i += 4) {
dataView.setUint32(i, d32);
}
})
);
console.log(
"dataView.setBigUint64",
benchmark(() => {
const d64 =
(102n << 56n) +
(102n << 48n) +
(102n << 40n) +
(102n << 32n) +
(102n << 24n) +
(102n << 16n) +
(102n << 8n) +
102n;
for (let i = 0; i < dataView.byteLength; i += 8) {
dataView.setBigUint64(i, d64);
}
})
);
console.log(
"Uint8Array",
benchmark(() => {
const u = new Uint8Array(
buffer.buffer,
buffer.byteOffset,
buffer.byteLength
);
for (let i = 0; i < u.length; i++) {
u[i] = 104;
}
})
);
console.log(
"Uint16Array",
benchmark(() => {
const u = new Uint16Array(
buffer.buffer,
buffer.byteOffset,
buffer.byteLength / 2
);
const d16 = (105 << 8) + 105;
for (let i = 0; i < u.length; i++) {
u[i] = d16;
}
})
);
console.log(
"Uint32Array",
benchmark(() => {
const u = new Uint32Array(
buffer.buffer,
buffer.byteOffset,
buffer.byteLength / 4
);
const d32 = (106 << 24) + (106 << 16) + (106 << 8) + 106;
for (let i = 0; i < u.length; i++) {
u[i] = d32;
}
})
);
console.log(
"BigUint64Array",
benchmark(() => {
const u = new BigUint64Array(
buffer.buffer,
buffer.byteOffset,
buffer.byteLength / 8
);
const d64 =
(107n << 56n) +
(107n << 48n) +
(107n << 40n) +
(107n << 32n) +
(107n << 24n) +
(107n << 16n) +
(107n << 8n) +
107n;
for (let i = 0; i < u.length; i++) {
u[i] = d64;
}
})
);
NodeJS version: v16.20.1
CPU name: Apple M1 Max
buffer[i] 1,626,555 ops/sec ±1.45% (3936768 samples)
dataView.setUint8 1,254,027 ops/sec ±0.2% (3205988 samples)
dataView.setUint16 2,517,510 ops/sec ±0.23% (5618905 samples)
dataView.setUint32 5,027,208 ops/sec ±0.06% (9018688 samples)
dataView.setBigUint64 858,113 ops/sec ±0.14% (2263468 samples)
Uint8Array 1,076,385 ops/sec ±0.1% (2822445 samples)
Uint16Array 2,092,201 ops/sec ±0.13% (4907031 samples)
Uint32Array 3,981,011 ops/sec ±0.14% (7791038 samples)
BigUint64Array 823,927 ops/sec ±0.18% (2197561 samples)
NodeJS version: v18.16.1
CPU name: Apple M1 Max
buffer[i] 89,115 ops/sec ±1.81% (262752 samples)
dataView.setUint8 1,228,235 ops/sec ±0.96% (3133067 samples)
dataView.setUint16 2,545,421 ops/sec ±0.11% (5685768 samples)
dataView.setUint32 5,023,411 ops/sec ±0.14% (9025550 samples)
dataView.setBigUint64 794,960 ops/sec ±0.16% (2126306 samples)
Uint8Array 94,597 ops/sec ±0.07% (279561 samples)
Uint16Array 186,971 ops/sec ±0.27% (544869 samples)
Uint32Array 369,759 ops/sec ±0.11% (1047989 samples)
BigUint64Array 405,929 ops/sec ±0.12% (1146368 samples)
NodeJS version: v20.10.0
CPU name: Apple M1 Max
buffer[i] 1,293,123 ops/sec ±0.1% (3338607 samples)
dataView.setUint8 314,684 ops/sec ±0.11% (902604 samples)
dataView.setUint16 620,340 ops/sec ±0.07% (1707187 samples)
dataView.setUint32 1,240,635 ops/sec ±0.09% (3219365 samples)
dataView.setBigUint64 722,984 ops/sec ±0.15% (1964150 samples)
Uint8Array 827,248 ops/sec ±0.17% (2218614 samples)
Uint16Array 1,592,141 ops/sec ±0.15% (3978597 samples)
Uint32Array 2,973,552 ops/sec ±0.14% (6528902 samples)
BigUint64Array 3,792,152 ops/sec ±0.26% (7737504 samples)
---
NodeJS version: v16.13.0
CPU name: Intel(R) Core(TM) i9-10850K CPU @ 3.60GHz
buffer[i] 2,328,576 ops/sec ±0.07% (4984854 samples)
dataView.setUint8 1,552,089 ops/sec ±0.07% (3629405 samples)
dataView.setUint16 3,657,630 ops/sec ±0.07% (6672451 samples)
dataView.setUint32 7,085,812 ops/sec ±0.11% (9475248 samples)
dataView.setUint64 808,382 ops/sec ±0.1% (2124204 samples)
Uint8Array 1,878,796 ops/sec ±0.12% (4250001 samples)
Uint16Array 2,990,457 ops/sec ±0.15% (5918355 samples)
Uint32Array 5,098,239 ops/sec ±0.17% (8083893 samples)
BigUint64Array 795,873 ops/sec ±0.11% (2093495 samples)
NodeJS version: v18.19.0
CPU name: Intel(R) Core(TM) i9-10850K CPU @ 3.60GHz
buffer[i] 106,874 ops/sec ±0.07% (313137 samples)
dataView.setUint8 1,538,306 ops/sec ±0.1% (3560966 samples)
dataView.setUint16 3,051,460 ops/sec ±0.11% (5815800 samples)
dataView.setUint32 5,840,756 ops/sec ±0.15% (8331019 samples)
dataView.setUint64 765,111 ops/sec ±0.1% (2006213 samples)
Uint8Array 103,226 ops/sec ±0.06% (303352 samples)
Uint16Array 203,168 ops/sec ±0.07% (586594 samples)
Uint32Array 394,335 ops/sec ±0.08% (1100577 samples)
BigUint64Array 438,242 ops/sec ±0.09% (1214529 samples)
NodeJS version: v20.10.0
CPU name: Intel(R) Core(TM) i9-10850K CPU @ 3.60GHz
buffer[i] 1,577,688 ops/sec ±0.1% (3755586 samples)
dataView.setUint8 350,924 ops/sec ±0.08% (995365 samples)
dataView.setUint16 674,149 ops/sec ±0.06% (1822017 samples)
dataView.setUint32 1,335,666 ops/sec ±0.07% (3297563 samples)
dataView.setUint64 726,197 ops/sec ±0.11% (1945192 samples)
Uint8Array 1,089,143 ops/sec ±0.09% (2761621 samples)
Uint16Array 1,978,450 ops/sec ±0.12% (4451498 samples)
Uint32Array 3,614,258 ops/sec ±0.17% (6756030 samples)
BigUint64Array 3,657,771 ops/sec ±0.25% (6846882 samples)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment