-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (164 loc) · 5.7 KB
/
index.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Elements that are selected and used repeatedly
const depthEle = document.getElementById('total__depth');
const wordEle = document.getElementById('total__words');
const nodeEle = document.getElementById('total__nodes');
const searchBox = document.getElementById('search__results');
const searchList = document.getElementById('search__results__list');
const input = document.getElementById('input');
const svg = document.getElementsByTagName('svg')[0];
const btnContainer = document.getElementById('search__btn-container');
const instructions = document.getElementById('instructions');
const about = document.getElementById('about');
const triangle = document.getElementById('triangle');
const metrics = document.getElementById('search__metrics');
// Variables and methods to modify user feedback as
// trie is being populated
window.highestTrieDepth = 0;
window.wordCount = 0;
window.nodeCount = 0;
const updateTrieDepth = () => {
depthEle.innerHTML = window.highestTrieDepth;
};
const updateWordCount = () => {
window.wordCount++;
wordEle.innerHTML = window.wordCount;
};
const updateNodeCount = () => {
window.nodeCount++;
nodeEle.innerHTML = window.nodeCount;
};
// puts dictionary arr into trie
const populateTrie = () => {
togglePopulateButtons();
// the much faster way to populate the trie which takes about 1.6 seconds
// window.dictionary.forEach(word => trie.addChild(word));
// slowing down trie population for dramatic effect in UX
let word = '';
window.interval = setInterval(() => {
if (window.wordCount >= window.dictionary.length - 1) {
clearInterval(window.interval);
}
word = dictionary[window.wordCount];
trie.addChild(word);
}, 0.000001);
};
const resetTrie = () => {
clearInterval(window.interval);
depthEle.innerHTML = wordEle.innerHTML = nodeEle.innerHTML = '0';
window.highestTrieDepth = window.wordCount = window.nodeCount = 0;
window.trie = new TrieNode('');
togglePopulateButtons();
};
// enable and disable populate trie buttons
const togglePopulateButtons = () => {
const populateButtons = document.getElementsByClassName('btn-populate');
const changeTo = !populateButtons[0].disabled;
for (let i = 0; i < populateButtons.length; i++) {
populateButtons[i].disabled = changeTo;
}
};
// input box search method
const search = () => {
clearSearch();
const val = input.value.toLowerCase();
if (val.length !== 0) {
const t0 = performance.now();
const words = trieLogSearch(val, window.trie);
const t1 = performance.now();
linearSearch(val, window.dictionary);
const t2 = performance.now();
// if there are any search results
if (words.length > 0) {
const tenWords = pullFirstTenVals(words);
btnContainer.style.display = 'none';
searchBox.style.display = '';
tenWords.forEach(word => {
const node = createLiTextNode(word, val);
searchList.appendChild(node);
});
const logTime = Math.round((t1 - t0) * 100) / 100;
const linTime = Math.round((t2 - t1) * 100) / 100;
const multiplier = Math.round(((t2 - t1) / (t1 - t0)) * 100) / 100;
if (multiplier === Infinity || isNaN(multiplier)) {
metrics.innerHTML =
'The browser you are using rounds its performance metrics up to the nearest millisecond. Please try using this feature in Google Chrome which will measure performance to a microsecond or lesser';
} else {
metrics.innerHTML = `Trie Search: ${logTime}ms Linear Search: ${linTime}ms ${multiplier}x faster`;
}
}
}
};
// clear search results for previous user input
// hides and shows search box and btn container
const clearSearch = () => {
while (searchList.firstChild) {
searchList.removeChild(searchList.firstChild);
}
btnContainer.style.display = '';
searchBox.style.display = 'none';
};
// helper function for search method
const createLiTextNode = (word, searchStr) => {
const firstHalf = word.slice(searchStr.length);
const node = document.createElement('LI');
const svgClone = svg.cloneNode(true);
node.appendChild(svgClone);
const textNode = document.createTextNode(searchStr);
node.appendChild(textNode);
const boldTextNode = createBTextNode(firstHalf);
node.appendChild(boldTextNode);
attachOnClick(node, word);
return node;
};
const createBTextNode = str => {
const node = document.createElement('B');
const textNode = document.createTextNode(str);
node.appendChild(textNode);
return node;
};
const splitWord = (searchStr, fullWord) => {
const first = fullWord.slice(searchStr.length);
const second = searchStr;
return [first, second];
};
// adds an onclick method to each search list item
const attachOnClick = (node, word) => {
node.onclick = () => {
input.value = word;
input.oninput();
};
};
// shows instructions and hides about
const toggleInstructions = () => {
if (about.classList.value.includes('info--show')) {
setTimeout(() => instructions.classList.add('info--show'), 100);
about.classList.remove('info--show');
} else {
if (instructions.classList.value.includes('info--show')) {
instructions.classList.remove('info--show');
} else {
instructions.classList.add('info--show');
}
}
};
// shows about and hides instructions
const toggleAbout = () => {
if (instructions.classList.value.includes('info--show')) {
setTimeout(() => about.classList.add('info--show'), 100);
instructions.classList.remove('info--show');
} else {
if (about.classList.value.includes('info--show')) {
about.classList.remove('info--show');
} else {
about.classList.add('info--show');
}
}
};
document.getElementById('form').addEventListener(
'submit',
e => {
search(input);
e.preventDefault();
},
false,
);