|
| 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 | +}())); |
0 commit comments