-
Notifications
You must be signed in to change notification settings - Fork 2
/
suite.js
58 lines (52 loc) · 1.51 KB
/
suite.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
"use strict";
module.exports = newSuite;
var benchmark = require("benchmark"),
chalk = require("chalk");
if (typeof(window) == 'object') {
window.Benchmark = benchmark;
}
var padSize = 34;
function newSuite(name) {
var benches = [];
return new benchmark.Suite(name)
.on("add", function(event) {
benches.push(event.target);
})
.on("start", function() {
console.log(
chalk.white.bold("benchmarking " + name + " performance ...") +
"\n");
})
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
if (benches.length > 1) {
benches.sort(function(a, b) {
return getHz(b) - getHz(a);
});
var fastest = benches[0],
fastestHz = getHz(fastest);
console.log("\n" + chalk.white(pad(fastest.name, padSize)) + " was " +
chalk.green("fastest"));
benches.slice(1).forEach(function(bench) {
var hz = getHz(bench);
var percent = 1 - hz / fastestHz;
console.log(
chalk.white(pad(bench.name, padSize)) + " was " +
chalk.red((percent * 100).toFixed(1) +
"% ops/sec slower (factor " + (fastestHz / hz).toFixed(2) +
")"));
});
}
console.log("\n");
});
}
function getHz(bench) {
return 1 / (bench.stats.mean + bench.stats.moe);
}
function pad(str, len, l) {
while (str.length < len)
str = l ? str + " " : " " + str;
return str;
}