Skip to content

Commit 3830f65

Browse files
committed
Add a benchmark suite for parsing and serializing source maps
1 parent f2c9df4 commit 3830f65

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

bench/bench.html

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Benchmark Source Maps</title>
5+
<meta charset="UTF-8" />
6+
<style>
7+
table {
8+
border-collapse: collapse;
9+
}
10+
td {
11+
border: 1px solid #ccc;
12+
padding: 1em;
13+
}
14+
thead td {
15+
font-weight: bold;
16+
}
17+
tbody td {
18+
font-family: monospace;
19+
text-align: right;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<h1>Benchmark Source Maps</h1>
25+
26+
<hr/>
27+
28+
<button id="bench-consumer">
29+
<h2>Benchmark Parsing Source Maps</h2>
30+
</button>
31+
<div id="consumer-results"></div>
32+
33+
<hr/>
34+
35+
<button id="bench-generator">
36+
<h2>Benchmark Serializing Source Maps</h2>
37+
</button>
38+
<div id="generator-results"></div>
39+
40+
<script src="jquery-2.0.1-source-map.js"></script>
41+
<script src="stats.js"></script>
42+
<script src="../dist/source-map.js"></script>
43+
<script src="bench.js"></script>
44+
</body>
45+
</html>

bench/bench.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Ensure that benchmarks don't get optimized away by calling this blackbox
2+
// function in your benchmark's action.
3+
window.__benchmarkResults = [];
4+
window.benchmarkBlackbox = [].push.bind(window.__benchmarkResults);
5+
6+
// Benchmark running an action n times.
7+
function benchmark(name, n, action) {
8+
window.__benchmarkResults = [];
9+
10+
// Warm up the JIT.
11+
for (var i = 0; i < n; i++) {
12+
action();
13+
}
14+
15+
var stats = new Stats("ms");
16+
17+
console.profile(name);
18+
for (var i = 0; i < n; i++) {
19+
var start = window.performance.now();
20+
action();
21+
stats.take(window.performance.now() - start);
22+
}
23+
console.profileEnd(name);
24+
25+
return stats;
26+
}
27+
28+
// Run a benchmark when the given button is clicked and display results in the
29+
// given element.
30+
function benchOnClick(button, results, name, n, action) {
31+
button.addEventListener("click", function (e) {
32+
e.preventDefault();
33+
var stats = benchmark(name, n, action);
34+
results.innerHTML = `
35+
<table>
36+
<thead>
37+
<tr>
38+
<td>Samples</td>
39+
<td>Total (${stats.unit})</th>
40+
<td>Mean (${stats.unit})</th>
41+
<td>Standard Deviation (%)</th>
42+
</tr>
43+
</thead>
44+
<tbody>
45+
<tr>
46+
<td>${n}</td>
47+
<td>${stats.total().toFixed(3)}</td>
48+
<td>${stats.mean().toFixed(3)}</td>
49+
<td>${stats.stddev().toFixed(2)}</td>
50+
</tr>
51+
</tbody>
52+
</table>
53+
`;
54+
}, false);
55+
}
56+
57+
var consumerButton = document.getElementById("bench-consumer");
58+
var consumerResults = document.getElementById("consumer-results");
59+
60+
benchOnClick(consumerButton, consumerResults, "parse source map", 100, function () {
61+
var smc = new sourceMap.SourceMapConsumer(window.jQuerySourceMap);
62+
benchmarkBlackbox(smc._generatedMappings);
63+
});
64+
65+
var generatorButton = document.getElementById("bench-generator");
66+
var generatorResults = document.getElementById("generator-results");
67+
68+
benchOnClick(generatorButton, generatorResults, "serialize source map", 100, (function () {
69+
var smc = new sourceMap.SourceMapConsumer(window.jQuerySourceMap);
70+
var smg = sourceMap.SourceMapGenerator.fromSourceMap(smc);
71+
return function () {
72+
benchmarkBlackbox(smg.toString());
73+
};
74+
}()));

bench/jquery-2.0.1-source-map.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bench/stats.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Original author: Jim Blandy
2+
3+
function Stats(unit) {
4+
this.unit = unit || "";
5+
this.x0 = this.x1 = this.x2 = 0;
6+
}
7+
8+
Stats.prototype.take = function (x) {
9+
this.x0 += 1;
10+
this.x1 += x;
11+
this.x2 += x*x;
12+
}
13+
14+
Stats.prototype.total = function() {
15+
return this.x1;
16+
}
17+
18+
Stats.prototype.mean = function () {
19+
return this.x1 / this.x0;
20+
}
21+
22+
Stats.prototype.stddev = function () {
23+
return Math.sqrt(this.x0 * this.x2 - this.x1 * this.x1) / (this.x0 - 1);
24+
}
25+
26+
Stats.prototype.toString = function () {
27+
return "[Stats " +
28+
"total: " + this.total() + this.unit + ", " +
29+
"mean: " + this.mean() + this.unit + ", " +
30+
"stddev: " + Math.ceil(this.stddev() * 100 / this.mean()) + "%]";
31+
}

0 commit comments

Comments
 (0)