Created
April 11, 2023 10:44
-
-
Save akirilyuk/7b050a7a85d5e490f08de77ef9162604 to your computer and use it in GitHub Desktop.
Perf compatison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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