-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (37 loc) · 1.08 KB
/
index.js
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
import * as Comlink from "https://unpkg.com/[email protected]/dist/esm/comlink.mjs";
const x = [4, 25698347, 25698341, 25698343, 25698349];
document.getElementById("worker").innerHTML += `All the values in x are:- ${x.join(" ")}<br />`;
async function start() {
const wrapped = Comlink.wrap(new Worker("./working.js"));
//the promise way
wrapped.primeCheck(x)
.then(ans => {
document.getElementById("worker").innerHTML += `All the primes in x are:- ${ans.join(" ")}<br />`;
}).catch((err) => {
console.error(err);
});
// The callback way
wrapped.dataProcessing("./data.csv", Comlink.proxy(ans => {
document.getElementById("worker").innerHTML += `Avg age:- ${ans}<br />`;
}));
}
const primeCheck = (x) => {
let t1 = Date.now();
x.forEach(number => {
let res = null;
for (let i = 2; i < number; i++) {
if (number % i === 0) {
res = i;
break;
}
}
if (res === null) {
console.log(`# - ${number} is prime`);
} else {
console.log(`# - ${number} is divisible by ${res}`);
}
});
console.log(`# - Total time : - ${Date.now() - t1}`);
}
start();
primeCheck(x);