/ static / index.js
index.js
 1  import init, { main } from "./pkg/wasm.js";
 2  
 3  function supportsServiceWorkers() {
 4    return "serviceWorker" in navigator;
 5  }
 6  
 7  /** @param {string} seriveWorkerPath */
 8  function registerServiceWorker(seriveWorkerPath) {
 9    if (!supportsServiceWorkers()) return;
10    navigator.serviceWorker.register(seriveWorkerPath);
11  }
12  
13  function fibonacci(n) {
14    return n < 1 ? 0 : n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2);
15  }
16  
17  // Register the service worker
18  registerServiceWorker("/service_worker.js");
19  
20  // Initialise and run WebAssembly
21  await init();
22  main();
23  
24  let number = 32;
25  let start = performance.now();
26  let calculationResult = fibonacci(number);
27  let duration = performance.now() - start;
28  
29  console.log(`Fibonacci of ${number} is:`, calculationResult);
30  console.log("Time elapsed in (JavaScript) fibonacci() is:", `${duration}ms`);
31  
32  // Fake a delay
33  await new Promise((resolve) => setTimeout(resolve, 2000));
34  
35  // A test fetch
36  const response = await fetch("/favicon.png");
37  const blob = await response.blob();
38  console.log(blob);