-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.hash.map.js
More file actions
61 lines (52 loc) · 1.65 KB
/
Copy pathbench.hash.map.js
File metadata and controls
61 lines (52 loc) · 1.65 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
var Benchmark = require('benchmark')
var suite = new Benchmark.Suite('Couting numbers')
const chalk = require('chalk')
console.log('------ prepare data')
let total = 10000// 10000000 // 10 million
const hashMap = new Map()
const hash = {}
for (let x = 0; x <= total; x++) {
hash[`key_${x}`] = `value_${x}`
hashMap.set(`key_${x}`, `value_${x}`)
}
// console.log('------ starting perf')
// console.log('numbers', numbers)
// add tests
suite
.add('hashMap with Map', function () {
function someFunction1() {
for (let index = 0; index < 100; index++) {
const value = hashMap.get(`key_${index}`)
}
}
someFunction1()
})
.add('classic hash with Object notation', function () {
function someFunction2() {
for (let index = 0; index < 100; index++) {
const value = hash[`key_${index}`]
}
}
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(`${this[0].name} is ${compare(fastest.map('hz'), this[1].hz)} than ${this[1].name}`));
})
// run async
.run({
async: true
})