aboutsummaryrefslogtreecommitdiff
path: root/src/talks/wasm/examples/4-primes-sieve/script.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/talks/wasm/examples/4-primes-sieve/script.js')
-rw-r--r--src/talks/wasm/examples/4-primes-sieve/script.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/talks/wasm/examples/4-primes-sieve/script.js b/src/talks/wasm/examples/4-primes-sieve/script.js
new file mode 100644
index 0000000..8041e5b
--- /dev/null
+++ b/src/talks/wasm/examples/4-primes-sieve/script.js
@@ -0,0 +1,38 @@
1import Primes from "./primes.mjs";
2
3var primes = await Primes();
4
5var input = document.getElementById("input");
6var wasm_button = document.getElementById("wasmButton");
7var js_button = document.getElementById("jsButton");
8var resultText = document.getElementById("resultText");
9
10var count_wasm = (n) => primes._count(n);
11
12var count_js = (n) => {
13 if (n < 2)
14 return 0;
15
16 var notprime = new Uint8Array(n+2);
17
18 var count = 0;
19 for (var i = 2; i < n; i++) {
20 count += 1 - notprime[i];
21 for (var j = 2*i; j < n; j += i)
22 notprime[j] = 1;
23 }
24
25 return count;
26}
27
28var timerun = (count, tag) => {
29 var n = Number(input.value);
30 var msg = "Counting primes less than " + n + " with " + tag;
31 console.time(msg);
32 var c = count(n);
33 console.timeEnd(msg);
34 resultText.innerText = "There are " + c + " primes less than " + n;
35}
36
37wasm_button.addEventListener("click", () => timerun(count_wasm, "WASM"));
38js_button.addEventListener("click", () => timerun(count_js, "JS"));

Generated with cgit - Back to sebastiano.tronto.net