script.js (971B)
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 is_prime = (n) => { 13 if (n < 2) 14 return false; 15 for (var i = 2; i * i <= n; i++) 16 if (n % i == 0) 17 return false; 18 return true; 19 } 20 21 var count_js = (n) => { 22 var count = 0; 23 for (var i = 0; i < n; i++) 24 count += is_prime(i); 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"));