This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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