WPC Class Example

This example demonstrates BigInt factorization in a separate browser thread using a WPC Class.

HelloWorker.ts
WorkerExample.tsx
1import { worker } from 'vovk';
2
3@worker()
4export default class HelloWorker {
5 /**
6 * Factorizes a large number into its prime factors
7 * @param number - the large number to factorize
8 */
9 static factorize(number: bigint): bigint[] {
10 let factors: bigint[] = [];
11 if (number < 2n) {
12 return [number];
13 }
14
15 while (number % 2n === 0n) {
16 factors.push(2n);
17 number /= 2n;
18 }
19
20 for (let i = 3n; i * i <= number; i += 2n) {
21 while (number % i === 0n) {
22 factors.push(i);
23 number /= i;
24 }
25 }
26
27 if (number > 1n) {
28 factors.push(number); // Remaining number is a prime factor
29 }
30
31 return factors;
32 }
33}
1import { worker } from 'vovk';
2
3@worker()
4export default class HelloWorker {
5 /**
6 * Factorizes a large number into its prime factors
7 * @param number - the large number to factorize
8 */
9 static factorize(number: bigint): bigint[] {
10 let factors: bigint[] = [];
11 if (number < 2n) {
12 return [number];
13 }
14
15 while (number % 2n === 0n) {
16 factors.push(2n);
17 number /= 2n;
18 }
19
20 for (let i = 3n; i * i <= number; i += 2n) {
21 while (number % i === 0n) {
22 factors.push(i);
23 number /= i;
24 }
25 }
26
27 if (number > 1n) {
28 factors.push(number); // Remaining number is a prime factor
29 }
30
31 return factors;
32 }
33}