Skip to content

Instantly share code, notes, and snippets.

@akirilyuk
Created April 11, 2023 10:44
Show Gist options
  • Select an option

  • Save akirilyuk/7b050a7a85d5e490f08de77ef9162604 to your computer and use it in GitHub Desktop.

Select an option

Save akirilyuk/7b050a7a85d5e490f08de77ef9162604 to your computer and use it in GitHub Desktop.
Perf compatison
const {StaticPool} = require('node-worker-threads-pool');
const task = (array) => {
for (let i = 0; i < array.length; i++) {
array[i] = array[i] ** 2;
}
};
const pool = new StaticPool({
size: 4,
task,
});
const measureTime = async (fn, data, name, iterations = 100) => {
let totalDuration = 0;
for (let i = 0; i < iterations; i++) {
const start = Date.now();
await fn(data);
totalDuration += Date.now() - start;
}
console.log(
'Needed ',
(totalDuration / iterations).toFixed(3),
'ms on avg for',
iterations,
'iterations for processing',
name
);
};
async function main() {
const sharedArray = new Float32Array(
new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * 2048)
);
sharedArray.fill(1.4);
await measureTime(pool.exec, sharedArray, 'threadpool');
sharedArray.fill(1.4);
await measureTime(task, sharedArray, 'normal run');
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment