Created
June 14, 2019 00:59
-
-
Save florenceshelley/f17d9c9bddda3d3f48c7ef0d56e7c90c to your computer and use it in GitHub Desktop.
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
const hashmap = (n) => { | |
const self = this; | |
self.list = [[]]; | |
self.has = (e) => { | |
if (self.list.length >= index + 1) { | |
return self.list[index].indexOf(e) >= 0; | |
} else { | |
return false; | |
} | |
}; | |
self.add = (e) => { | |
index = e.toString().split('').map(c => c.charCodeAt(0)).reduce((a,b)=>(a+b))%(Math.pow(n, 0.5)|0); | |
if (self.list.length >= index + 1) { | |
self.list[index].push(e); | |
} else { | |
while(self.list.length < index + 1) { | |
self.list.push([]); | |
} | |
self.list[index].push(e); | |
} | |
} | |
return self | |
} | |
m = hashmap(10000); | |
for (let i = 0; i < 10000; i++) m.add(i); | |
start = Date.now() | |
for (let i = 0; i < 10000; i++) m.has(i); | |
console.log(Date.now()-start) | |
console.log(m); | |
// 1 M elements | |
// how long does it take to find 1 of 1M elements on average? | |
// on average 1M checks to find | |
// what if we have 1000 lists of 1000 items and we know which list its in? | |
// 1 check to get the list index + 1000 checks worst case |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment