script.js (961B)
1 import Primes from "./primes.mjs"; 2 3 var primes = await Primes(); 4 5 var input = document.getElementById("input"); 6 var wasm_button = document.getElementById("wasmButton"); 7 var js_button = document.getElementById("jsButton"); 8 var resultText = document.getElementById("resultText"); 9 10 var count_wasm = (n) => primes._count(n); 11 12 var 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 28 var 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 37 wasm_button.addEventListener("click", () => timerun(count_wasm, "WASM")); 38 js_button.addEventListener("click", () => timerun(count_js, "JS"));