Skip to content

Instantly share code, notes, and snippets.

@coyotte508
Last active Aug 4, 2022
Embed
What would you like to do?
/* 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