forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.html
88 lines (79 loc) · 3.02 KB
/
bench.html
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!doctype html>
<head>
<meta charset="utf-8">
<title>Acorn benchmark</title>
<script src="../acorn.js"></script>
<script src="compare/esprima.js"></script>
<script src="compare/uglifyjs.js"></script>
<script src="jquery-string.js"></script>
<script src="codemirror-string.js"></script>
<style>
td { text-align: right; padding-right: 20px; }
th { text-align: left; padding-right: 40px; }
body { max-width: 50em; padding: 1em 2em; }
h1 { font-size: 150%; }
</style>
</head>
<h1>Acorn/Esprima/UglifyJS speed comparison</h1>
<p>This will run each of the three parsers on the source code of
jQuery 1.6.4 and CodeMirror 3.0b1 for two seconds, and show a table
indicating the number of lines parsed per second. Note that UglifyJS
always stores location data, and is thus not fairly compared by the
benchmark <em>without</em> location data.<p>
<p>Also note that having the developer tools open in Chrome, or
Firebug in Firefox <em>heavily</em> influences the numbers you get. In
Chrome, the effect even lingers (in the tab) after you close the
developer tools. Load in a fresh tab to get (halfway) stable
numbers.</p>
<button onclick="run(false)">Compare <strong>without</strong> location data</button>
<button onclick="run(true)">Compare <strong>with</strong> location data</button>
<span id="running"></span>
<script>
function runAcorn(code, locations) {
acorn.parse(code, {locations: locations});
}
function runEsprima(code, locations) {
esprima.parse(code, {loc: locations});
}
function runUglifyJS(code) {
uglifyjs.parse(code);
}
var totalLines = codemirror30.split("\n").length + jquery164.split("\n").length;
function benchmark(runner, locations) {
// Give it a chance to warm up (first runs are usually outliers)
runner(jquery164, locations);
runner(codemirror30, locations);
var t0 = +new Date, t1, lines = 0;
for (;;) {
runner(jquery164, locations);
runner(codemirror30, locations);
lines += totalLines;
t1 = +new Date;
if (t1 - t0 > 2000) break;
}
return lines / ((t1 - t0) / 1000);
}
function showOutput(values) {
var html = "<hr><table>";
for (var i = 0; i < values.length; ++i)
html += "<tr><th>" + values[i].name + "</td><td>" + Math.round(values[i].score) + " lines per second</td><td>" +
Math.round(values[i].score * 100 / values[0].score) + "%</td></tr>";
document.body.appendChild(document.createElement("div")).innerHTML = html;
}
function run(locations) {
var running = document.getElementById("running");
running.innerHTML = "Running benchmark...";
var data = [{name: "Acorn", runner: runAcorn},
{name: "Esprima", runner: runEsprima},
{name: "UglifyJS", runner: runUglifyJS}];
var pos = 0;
function next() {
data[pos].score = benchmark(data[pos].runner, locations);
if (++pos == data.length) {
running.innerHTML = "";
showOutput(data);
} else setTimeout(next, 100);
}
setTimeout(next, 50);
}
</script>