-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.js
53 lines (43 loc) · 1.24 KB
/
benchmark.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
const Benchmark = require('benchmark')
const FixedSet = require('./index')
const { uniqueNamesGenerator, adjectives, colors, animals } = require('unique-names-generator')
console.log('Benchmark of FixedSet vs Set vs object\n\n')
const TEST_COUNT = 100
console.log('Generating ' + TEST_COUNT + ' test values...')
const TEST_VALUES = (new Array(TEST_COUNT).fill('')).map(function () {
return uniqueNamesGenerator({
dictionaries: [adjectives, animals, colors],
length: 2
})
})
console.log('Initializing benchmark...')
var suite = new Benchmark.Suite;
var fixedSet = new FixedSet(...TEST_VALUES)
var realSet = new Set(...TEST_VALUES)
var obj = {}
// needle, assume worst-case
var needle = TEST_VALUES[TEST_VALUES.length - 1]
TEST_VALUES.forEach(function (value) {
obj[value] = 1
})
console.log('Running benchmark...\n\n')
// add tests
suite
.add('Set#has', function() {
realSet.has(needle)
})
.add('object', function() {
obj[needle]
})
.add('FixedSet#has', function() {
fixedSet.has(needle)
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target))
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
// run async
.run({ 'async': false })