forked from benthemonkey/source-map
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbench.js
74 lines (61 loc) · 1.8 KB
/
bench.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
function noop() {}
if (typeof console === "undefined") {
console = {};
}
if (!console.time) {
console.time = console.timeEnd = noop;
}
if (!console.profile) {
console.profile = console.profileEnd = noop;
}
// Ensure that benchmarks don't get optimized away by calling this blackbox
// function in your benchmark's action.
var __benchmarkResults = [];
var benchmarkBlackbox = [].push.bind(__benchmarkResults);
// Benchmark running an action n times.
function benchmark(name, setup, action) {
__benchmarkResults = [];
setup();
// Warm up the JIT.
var start = Date.now();
while ((Date.now() - start) < 5000 /* 5 seconds */) {
action();
}
var stats = new Stats("ms");
console.profile(name);
while ((Date.now() - start) < 30000 /* 30 seconds */) {
console.time("iteration");
var thisIterationStart = Date.now();
action();
stats.take(Date.now() - thisIterationStart);
console.timeEnd("iteration");
}
console.profileEnd(name);
return stats;
}
var EXPECTED_NUMBER_OF_MAPPINGS = 2350714;
var smg = null;
function benchmarkSerializeSourceMap() {
return benchmark(
"serialize source map",
function () {
if (!smg) {
var smc = new sourceMap.SourceMapConsumer(testSourceMap);
smg = sourceMap.SourceMapGenerator.fromSourceMap(smc);
}
},
function () {
benchmarkBlackbox(smg.toString());
}
);
}
function benchmarkParseSourceMap() {
return benchmark("parse source map", noop, function () {
var smc = new sourceMap.SourceMapConsumer(testSourceMap);
if (smc._generatedMappings.length !== EXPECTED_NUMBER_OF_MAPPINGS) {
throw new Error("Expected " + EXPECTED_NUMBER_OF_MAPPINGS + " mappings, found "
+ smc._generatedMappings.length);
}
benchmarkBlackbox(smc._generatedMappings.length);
});
}