Skip to content

Instantly share code, notes, and snippets.

@muhozi
Created October 30, 2020 23:38
Show Gist options
  • Save muhozi/2280bf41730f354f600d3fbb520d0dc6 to your computer and use it in GitHub Desktop.
Save muhozi/2280bf41730f354f600d3fbb520d0dc6 to your computer and use it in GitHub Desktop.
If you model your data to use integers instead of strings for hashmap keys, you can avoid a lot of cloning/hashing and get at least 20x perf boost!
let strings = [];
numbers = [];
for (let i = 0; i < 1000000; i++) {
strings.push(`${i}`.repeat(100));
numbers.push(i);
}
(() => {
console.time('numbers');
const result = {};
for (number of numbers) {
result[number] = 1;
}
console.timeEnd('numbers');
})();
(() => {
console.time('strings');
const result = {};
for (string of strings) {
result[string] = 1;
}
console.timeEnd('strings');
})();
@muhozi
Copy link
Author

muhozi commented Oct 30, 2020

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