-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo-wasm.html
74 lines (69 loc) · 2.06 KB
/
demo-wasm.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
<html>
<head>
<title>Demo some wasm</title>
</head>
<body>
<h1>Web assembly demo</h1>
<div>
Status: <span id="status"></span><br/>
Input: <input type="text" id="input" name="input" value="32"/>
<button onclick="click_ctof();">C to F</button>
<button onclick="click_ftoc();">F to C</button>
<button onclick="click_fib();">Fib</button>
<button onclick="click_fibjs();">Fib (JS)</button>
<br/>
Output: <span id="output"></span><br/>
</div>
<script src="load.js"></script>
<script>
window.onload = () => {
window.examples_module = load_wasm("examples.wasm", message => {
document.getElementById("status").innerText = message;
}
);
}
async function calculate(name) {
const input = document.getElementById("input");
const output = document.getElementById("output");
const mod = await examples_module;
benchmark(() => {
output.innerText = mod[name](input.value);
});
}
async function click_ctof() {
calculate("ctof");
}
async function click_ftoc() {
calculate("ftoc");
}
async function click_fib() {
calculate("fib");
}
function click_fibjs() {
const input = document.getElementById("input");
const output = document.getElementById("output");
benchmark(() => {
output.innerText = fib(input.value);
})
}
function fib(n) {
if (n < 2) {
return 1;
} else {
return fib(n-1) + fib(n-2);
}
}
function benchmark(f) {
const status = document.getElementById("status");
performance.mark("start");
f();
performance.mark("end");
performance.measure("duration", "start", "end");
status.innerText = performance.getEntriesByName("duration",
"measure")[0].duration + "ms";
performance.clearMarks();
performance.clearMeasures();
}
</script>
</body>
</html>