-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.array.vs.map.js
More file actions
69 lines (59 loc) · 1.73 KB
/
bench.array.vs.map.js
File metadata and controls
69 lines (59 loc) · 1.73 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
var Benchmark = require('benchmark')
var suite = new Benchmark.Suite('Couting numbers')
const chalk = require('chalk')
console.log('------ prepare data')
let total = 100000// 10000000 // 10 million
const arrs = []
let strings = ''
for (let x = 0; x <= total; x++) {
arrs.push([x, x])
strings = strings + `${x},`
}
const store = new Map(arrs)
// add tests
suite
.add('using Map as Store', function () {
function someFunction1() {
for (let x = 0; x <= total; x++) {
const val = store.get(x)
}
}
someFunction1()
})
.add('using Array as Store', function () {
function someFunction2() {
for (let x = 0; x <= total; x++) {
const val = arrs[x]
}
}
someFunction2()
})
.add('using String as Store', function () {
function someFunction2() {
for (let x = 0; x <= total; x++) {
const val = strings[x]
}
}
someFunction2()
})
// add listeners
.on('cycle', function (event) {
console.log(chalk.magenta(String(event.target)))
})
.on('complete', function () {
// console.log(this.filter('fastest'))
const fastest = this.filter('fastest')
console.log(chalk.green('Fastest is ' + fastest.map('name')))
function compare(a, b) {
if (a > b)
return (a / b * 100).toFixed() + '% faster';
if (a == b)
return "the same";
return (b / a * 100).toFixed() + '% slower';
}
console.log(chalk.blue(`${fastest.map('name')} is ${compare(fastest.map('hz'), this[1].hz)} than ${this[0].name}`));
})
// run async
.run({
async: true
})