forked from ryansolid/circles-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
77 lines (70 loc) · 2.45 KB
/
client.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
75
76
77
FRAMEWORKS = [
{ name: "Backbone", version: "1.4.0" },
{ name: "Inferno", version: "7.1.11" },
{ name: "Knockout", version: "3.5.0" },
{ name: "LitHTML", version: "1.0.0" },
{ name: "Preact", version: "10.0.1" },
{ name: "React", version: "16.8.6" },
{ name: "Solid", version: "1.0.0" },
{ name: "SolidStore", version: "1.0.0" },
{ name: "Svelte", version: "3.28.0" },
{ name: "Surplus", version: "0.5.3" },
{ name: "Vanilla", version: "" }
];
Benchmark = {
number: 300,
Framework: {}
};
(function() {
const buttons = document.getElementById("buttons");
const timing = document.getElementById("timing");
const running = document.getElementById("running");
let grid = document.getElementById("grid");
let timer, loopTime, loopCount, totalTime, currentLoop, currentImpl;
function benchLoop() {
let lastLoopTime = loopTime || performance.now();
loopTime = performance.now();
loopCount++;
currentLoop();
totalTime = totalTime + loopTime - lastLoopTime;
if (loopCount % 30 === 0)
timing.textContent = `Performed ${loopCount} iterations in ${totalTime.toFixed(
2
)} ms (average ${(totalTime / loopCount).toFixed(2)} ms per loop).`;
timer = setTimeout(benchLoop, 0);
}
function stop() {
timer && clearTimeout(timer);
currentImpl && currentImpl.cleanup && currentImpl.cleanup();
running.textContent = "None";
let newGrid = document.createElement("div");
newGrid.id = "grid";
document.body.replaceChild(newGrid, grid);
grid = newGrid;
}
FRAMEWORKS.forEach(framework => {
const script = document.createElement("script");
script.src = `dist/${framework.name.toLowerCase()}.js`;
script.type = "text/javascript";
document.head.appendChild(script);
const button = document.createElement("button");
button.textContent = `${framework.name} ${framework.version}`;
button.onclick = () => {
stop();
loopTime = loopCount = totalTime = 0;
let impl;
if ((impl = Benchmark.Framework[framework.name])) {
currentImpl = impl;
running.textContent = framework.name;
impl.start();
currentLoop = impl.loop;
benchLoop();
}
};
buttons.appendChild(button);
});
const button = document.createElement("button");
button.textContent = "Stop Benchmark";
button.onclick = () => stop();
buttons.appendChild(button);
})();