Skip to content

Instantly share code, notes, and snippets.

@coyotte508
Last active August 4, 2022 14:37
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 coyotte508/a66c1c4f26e137d1f9baab78b8185bed to your computer and use it in GitHub Desktop.
Save coyotte508/a66c1c4f26e137d1f9baab78b8185bed to your computer and use it in GitHub Desktop.
/* eslint-disable github/array-foreach */
const bytes = new Uint8Array(10_000);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = Math.floor(Math.random() * 256);
}
const set = new Set();
const ITERATIONS = 100_000;
function buffer() {
for (let i = 0; i < ITERATIONS; i++) {
set.add(Buffer.from(bytes).toString("base64"));
}
}
function forEach() {
for (let i = 0; i < ITERATIONS; i++) {
const x = [];
bytes.forEach(ch => x.push(String.fromCharCode(ch)));
set.add(Buffer.from(x.join(""), "binary").toString("base64"));
}
}
function loop() {
for (let i = 0; i < ITERATIONS; i++) {
const x = [];
for (const char of bytes) {
x.push(String.fromCharCode(char));
}
set.add(Buffer.from(x.join(""), "binary").toString("base64"));
}
}
console.log(set.size);
console.time("loop");
loop();
console.timeEnd("loop");
console.log(set.size);
console.time("forEach");
forEach();
console.timeEnd("forEach");
console.log(set.size);
console.time("buffer");
buffer();
console.timeEnd("buffer");
console.log(set.size);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment