Created
October 30, 2020 23:38
-
-
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!
This file contains hidden or 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
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'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit: https://twitter.com/aarondjents/status/1322307291941642246?s=20