-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestHashFunction.js
More file actions
70 lines (62 loc) · 1.99 KB
/
testHashFunction.js
File metadata and controls
70 lines (62 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const
blue = "\x1b[34m",
red = "\x1b[31m",
green = "\x1b[32m",
white = "\x1b[37m",
yellow = "\x1b[33m",
cyan = "\x1b[36m",
reset = "\x1b[0m";
module.exports = {
testHash(hashFunction) {
/**
* Words are stored as : {"word1": 1, "word2":1, ...}
* To get words, we extract keys front this structure to get a list of Strings
* @type {Array} List of words
*/
time('load words');
const words = Object.keys(
require('./words_dictionary.json')
);
timeEnd('load words');
console.warn("You are about to work on ", green, words.length, reset, " words");
const results = new Map();
time('iterate over words');
for(let word of words) {
const hash = hashFunction(word);
const actualWords = results.get(hash);
if (actualWords) {
actualWords.push(word);
} else {
results.set(hash, [word]);
}
}
time('Tell the result');
if (results.size < words.length) {
for(let [hash, words] of results.entries()) {
if (words.length > 1) {
console.log(
"Found: ", white,
words.slice(0, 10).join(', '), words.length > 10 ? ", ...":"",
reset,
" for ", cyan, hash, reset,
" so ", red, words.length, reset, "collisions");
}
}
const collisionCount = words.length - results.size;
console.log("There was a total of ", red, collisionCount, reset, "collisions");
} else {
console.log(green, "WOW, no collision", reset);
}
}
}
function timeKey(key) {
return `${key} took ${blue}`;
}
function time(key) {
console.time(timeKey(key));
console.log(reset);
}
function timeEnd(key) {
console.timeEnd(timeKey(key));
console.log(reset);
}