Skip to content

Instantly share code, notes, and snippets.

@colinbellino
Created October 19, 2022 14:39
Show Gist options
  • Save colinbellino/6229144e376104ad51c1c1f1e7278410 to your computer and use it in GitHub Desktop.
Save colinbellino/6229144e376104ad51c1c1f1e7278410 to your computer and use it in GitHub Desktop.
Performance test to compare different ways to extract the unique items from a big array (in JavaScript)
const input = Array(7_000_000)
.fill("")
.map((_) => Math.floor(Math.random() * 5));
main();
function main() {
const count = 1000;
console.log("Running %d iterations", count);
run_test("FOR ", unique_with_for, count);
run_test("SET ", unique_with_set, count);
run_test("FOR_OF", unique_with_for_of, count);
run_test("REDUCE", unique_with_reduce, count);
}
function run_test(label, fn, count) {
let result = null;
let total = 0n;
let lower = 99999999999n;
let higher = 0n;
for (let i = 0; i < count; i++) {
const start = process.hrtime.bigint();
result = fn(input);
const end = process.hrtime.bigint(start);
const duration = end - start;
if (duration < lower) {
lower = duration;
}
if (duration > higher) {
higher = duration;
}
total += duration;
// console.log("Done in %d nanoseconds :", end - start, result);
}
console.log(
label + " --- Average: %dms | Lower: %dms | Higher: %dms | Total: %dms",
parseInt(total / BigInt(count) / BigInt(1_000_000)),
parseInt(lower / BigInt(1_000_000)),
parseInt(higher / BigInt(1_000_000)),
parseInt(total / BigInt(1_000_000))
);
// console.log(result);
}
function unique_with_set(input) {
return [...new Set(input)];
}
function unique_with_reduce(input) {
return input.reduce((result, current) => {
if (result.includes(current)) return result;
return [...result, current];
}, []);
}
function unique_with_for(input) {
const result = [];
let existing = {};
for (let entry_index = 0; entry_index < input.length; entry_index++) {
const entry = input[entry_index];
if (existing[entry]) continue;
existing[entry] = true;
result.push(entry);
}
return result;
}
function unique_with_for_of(input) {
const result = [];
let existing = {};
for (const entry of input) {
if (existing[entry]) continue;
existing[entry] = true;
result.push(entry);
}
return result;
}
@colinbellino
Copy link
Author

unique_results

@colinbellino
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment