1 const WorkerFunctions
= {
2 jsonIntegerSerialization
: 'jsonIntegerSerialization',
3 fibonacci
: 'fibonacci',
7 async
function runPoolifierTest (pool
, { tasks
, workerData
}) {
8 return new Promise((resolve
, reject
) => {
10 for (let i
= 1; i
<= tasks
; i
++) {
15 if (executions
=== tasks
) {
16 return resolve('FINISH')
28 function jsonIntegerSerialization (n
) {
29 for (let i
= 0; i
< n
; i
++) {
37 function generateRandomInteger (max
, min
= 0) {
41 return Math
.floor(Math
.random() * (max
- min
+ 1)) + min
43 return Math
.floor(Math
.random() * (max
+ 1))
47 * Intentionally inefficient implementation.
49 * @param {number} n - The number of fibonacci numbers to generate.
50 * @returns {number} - The nth fibonacci number.
52 function fibonacci (n
) {
54 return fibonacci(n
- 1) + fibonacci(n
- 2)
58 * Intentionally inefficient implementation.
60 * @param {number} n - The number to calculate the factorial of.
61 * @returns {number} - The factorial of n.
63 function factorial (n
) {
67 return factorial(n
- 1) * n
71 function executeWorkerFunction (data
) {
72 switch (data
.function) {
73 case WorkerFunctions
.jsonIntegerSerialization
:
74 return jsonIntegerSerialization(data
.n
|| 1000)
75 case WorkerFunctions
.fibonacci
:
76 return fibonacci(data
.n
|| 50)
77 case WorkerFunctions
.factorial
:
78 return factorial(data
.n
|| 1000)
80 throw new Error('Unknown worker function')
84 const LIST_FORMATTER
= new Intl
.ListFormat('en-US', {
92 executeWorkerFunction
,
93 generateRandomInteger
,