📜 ⬆️ ⬇️

64-bit hashmap in js

How would you do a quick hashmap for 64-bit keys? If the keys are 32-bit, then you can hardly do something faster than the built-in ES6 Map , but if the keys are 64-bit, then the complexity begins because JS is stuck on a 32-bit int .



I came across this task when I had to compare two rather large memory dumps: a text file on the left where each line is a 64-bit address and a variable type, and a similar file on the right. Each file contains approximately one million addresses and only 50 thousand of them are different.


For the sake of interest, I wrote several simple hashmaps and compared them on a similar task: two arrays of key-value pairs are given, where the key is two 32-bit numbers, both arrays are more than a million elements and the difference between them is 10-100 thousand elements - you need to find this difference and group it by values. It turned out that the fastest is not a built-in Map at all but a self-written (on JS) standard hashmap with lists for keys with identical hashes.


https://github.com/cd48b153/hashmap-contest


1M-50K2M-100K3M-5K
es6-concat1.000.980.81
es6-stack0.570.490.55
hashmap1.952.131.99
list0.410.270.25
naive-stack1.331.271.33
naive1.001.001.00
trie0.750.740.58

The construction of the Map<Map<int, string>> view Map<Map<int, string>> turns out to be 2 times slower than the self-defined hashmap. The "naive" hasmap based on {} taken as a reference point. Npm.js hashtable which is written in C ++ and uses std :: unordered_map turned out to be the slowest. It is not in the table because it corrupts the memory and crashes the node, but on average its speed was at 2.5, that is 2.5 times slower than the naive {} hashmap.


Can anyone do it faster than my own hashmap? PRs welcome :)


')

Source: https://habr.com/ru/post/360533/


All Articles