-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
74 lines (62 loc) · 1.61 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
//====================================================================
var ca = require('./');
//--------------------------------------------------------------------
var Benchmark = require('benchmark');
//====================================================================
var makeSimpleGetter = function (key) {
return function (obj) {
return obj[key];
};
};
var makeNestedGetter = function (path) {
return function (obj) {
var i, n, val;
val = obj;
for (i = 0, n = path.length; i < n; ++i) {
val = val[path[i]];
}
return val;
};
};
//====================================================================
var obj = {
foo: 'bar',
bar: {
baz: 'foo',
},
};
var simpleGetter = makeSimpleGetter('foo');
var nestedGetter = makeNestedGetter(['bar', 'baz']);
var caSimpleGetter = ca.getter('foo');
var caNestedGetter = ca.getter(['bar', 'baz']);
new Benchmark.Suite()
.add('simpleGetter()', function () {
return simpleGetter(obj);
})
.add('caSimpleGetter', function () {
return caSimpleGetter(obj);
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fatest is', this.filter('fastest').pluck('name'));
})
.run()
;
new Benchmark.Suite()
.add('nestedGetter()', function () {
return nestedGetter(obj);
})
.add('caNestedGetter()', function () {
return caNestedGetter(obj);
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fatest is', this.filter('fastest').pluck('name'));
})
.run()
;