Skip to content

Commit a3d80f6

Browse files
committed
Factor out setup from the benchmark code so that loading bench.html doesn't require parsing the test source map
1 parent 4b96b1d commit a3d80f6

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

bench/bench.js

+24-20
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ window.__benchmarkResults = [];
44
window.benchmarkBlackbox = [].push.bind(window.__benchmarkResults);
55

66
// Benchmark running an action n times.
7-
function benchmark(name, action) {
7+
function benchmark(name, setup, action) {
88
window.__benchmarkResults = [];
9+
setup();
910

1011
// Warm up the JIT.
1112
var start = Date.now();
@@ -29,10 +30,10 @@ function benchmark(name, action) {
2930

3031
// Run a benchmark when the given button is clicked and display results in the
3132
// given element.
32-
function benchOnClick(button, results, name, action) {
33+
function benchOnClick(button, results, name, setup, action) {
3334
button.addEventListener("click", function (e) {
3435
e.preventDefault();
35-
var stats = benchmark(name, action);
36+
var stats = benchmark(name, setup, action);
3637
results.innerHTML = `
3738
<table>
3839
<thead>
@@ -56,21 +57,24 @@ function benchOnClick(button, results, name, action) {
5657
}, false);
5758
}
5859

59-
var consumerButton = document.getElementById("bench-consumer");
60-
var consumerResults = document.getElementById("consumer-results");
60+
benchOnClick(document.getElementById("bench-consumer"),
61+
document.getElementById("consumer-results"),
62+
"parse source map",
63+
function () {},
64+
function () {
65+
var smc = new sourceMap.SourceMapConsumer(window.testSourceMap);
66+
benchmarkBlackbox(smc._generatedMappings);
67+
});
6168

62-
benchOnClick(consumerButton, consumerResults, "parse source map", function () {
63-
var smc = new sourceMap.SourceMapConsumer(window.testSourceMap);
64-
benchmarkBlackbox(smc._generatedMappings);
65-
});
66-
67-
var generatorButton = document.getElementById("bench-generator");
68-
var generatorResults = document.getElementById("generator-results");
69-
70-
benchOnClick(generatorButton, generatorResults, "serialize source map", (function () {
71-
var smc = new sourceMap.SourceMapConsumer(window.testSourceMap);
72-
var smg = sourceMap.SourceMapGenerator.fromSourceMap(smc);
73-
return function () {
74-
benchmarkBlackbox(smg.toString());
75-
};
76-
}()));
69+
benchOnClick(document.getElementById("bench-generator"),
70+
document.getElementById("generator-results"),
71+
"serialize source map",
72+
function () {
73+
if (!window.smg) {
74+
var smc = new sourceMap.SourceMapConsumer(window.testSourceMap);
75+
window.smg = sourceMap.SourceMapGenerator.fromSourceMap(smc);
76+
}
77+
},
78+
function () {
79+
benchmarkBlackbox(window.smg.toString());
80+
});

0 commit comments

Comments
 (0)