-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (37 loc) · 1.04 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
import Trie from './trie.js';
import Statistics from './statistics.js';
import data from './data.js';
let words = [];
for(let i = 0; i < 2000; i++) {
words = words.concat(data);
}
console.log('word count: ', words.length)
const prefix = "mo";
/**
* Method 1: check each string in the list and return all the words that start with specific chars
*/
const searchWordList = () => {
let wordsFound = [];
words.forEach( word => {
if( word.substr(0, prefix.length) === prefix )
wordsFound.push(word);
});
return wordsFound.join(', ');
}
//construct trie
let trie = new Trie(words);
/**
* Method 2: traverse the trie to find matching words
*/
const searchWordsTrie = () => trie.predict(prefix).getMatches();
/**
* RESULTS
*/
let m1 = new Statistics(searchWordList);
let r1 = m1.getTimeDelta();
console.log('Method 1: ', r1, 'milliseconds');
console.log(m1.getUnique())
let m2 = new Statistics(searchWordsTrie);
let r2 = m2.getTimeDelta();
console.log('Method 2: ', r2, 'milliseconds');
console.log(m2.words)