-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
71 lines (58 loc) · 1.76 KB
/
index.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
var Benchmark = require('benchmark');
function log(message) {
if (typeof window !== 'undefined') {
var div = document.createElement('div');
div.textContent = message;
document.getElementById('output').appendChild(div);
} else {
console.log(message);
}
return message.length;
}
function pad(input, count, padder) {
var length = String(input).length;
return input + new Array(Math.max(1, count + 1 - length)).join(padder);
}
function padRight(input, count, padder) {
var length = String(input).length;
return new Array(Math.max(1, count + 1 - length)).join(padder) + input;
}
function n(number) {
var result = Benchmark.formatNumber(number);
var match = result.match(/(.*)\.(\d{0,2}).*$/, '');
if (!match) { return result; }
var full = match[1];
var dec = match[2];
return full + '.' + pad(dec, 2, '0');
}
module.exports = function(suites) {
var suite = new Benchmark.Suite();
log('testing');
var nameWidth = 0;
var lineItemWidth = 0;
suites.forEach(function(s) {
var length = log('- ' + s.name);
nameWidth = Math.max(length, nameWidth);
suite.add(s);
});
suite.on('cycle', function(event) {
var length = log(' ' + pad(event.target.name + ' ',
nameWidth + 1, '.') +
padRight(' ' + n(event.target.hz, 1e2), 15, '.') +
' op/s')
lineItemWidth = Math.max(length, lineItemWidth);
})
.on('complete', function() {
pad('', lineItemWidth, '-');
console.log('fastest: ' + this.filter('fastest').map('name'));
})
.on('error', function (err) {
console.error(err);
});
setTimeout(function() {
console.log('running first test, please wait...')
suite.run({
'async': true
});
}, 1000);
}