-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtrieGetTest.js
71 lines (62 loc) · 2 KB
/
trieGetTest.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
/**
* A performance test file to compare the difference in performance of getting
* from the Trie when using limits. This can be run by
* `node trieGetTest.js`
*/
var TrieSearch = require('./src/TrieSearch');
var dict = require('./dictionary.json');
const NUM_ITERATIONS = 100
/**
* Function to time a block of code. Returns avg time to execute the given function.
* @param {*} f - Function to time
* @param {*} count - Number of iterations to continously run the block of code
*/
function timeFunction(f, count) {
let results = []
for (let i = 0; i < count; i++) {
let startTime = new Date().getTime()
f()
let endTime = new Date().getTime()
results.push(endTime - startTime)
}
let timeSum = 0
results.map((r) => {
timeSum += r
})
return timeSum / count
}
// Turn off caching
var ts = new TrieSearch(
null,
{
cache: false,
}
);
ts.addFromObject(dict);
console.log('Dictionary loaded into TrieSearch.');
// Testing without limit
// NOTE: Choosing s since that seems to have the largest number of results - 9950
let avgTimeWithoutLimit = timeFunction(function () {
ts.get('s')
}, NUM_ITERATIONS)
console.log("Avg time without-limit \t\t - ", avgTimeWithoutLimit, " ms")
// Testing with limit
var avgTimeWithLimit = timeFunction(function () {
ts.get('s', null, 10)
}, NUM_ITERATIONS)
console.log("Avg time with-limit 10 \t\t - ", avgTimeWithLimit, " ms")
// Testing with limit 100
var avgTimeWithLimit = timeFunction(function () {
ts.get('s', null, 100)
}, NUM_ITERATIONS)
console.log("Avg time with-limit 100 \t - ", avgTimeWithLimit, " ms")
// Testing with limit 1000
var avgTimeWithLimit = timeFunction(function () {
ts.get('s', null, 1000)
}, NUM_ITERATIONS)
console.log("Avg time with-limit 1000 \t - ", avgTimeWithLimit, " ms")
// Testing with limit 10000
var avgTimeWithLimit = timeFunction(function () {
ts.get('s', null, 10000)
}, NUM_ITERATIONS)
console.log("Avg time with-limit 10k \t - ", avgTimeWithLimit, " ms")