How to use Web Workers on Node.js? #635
-
|
Hey, I created a small "perf" test (see https://github.com/galdebert/zip.js/tree/tests-perf). on windows, 16 hw threads, nodejs v22.17.1{ "cfg": { "useWebWorkers": true, "level": 1 }, "results": { "zip_sec": "17.62", "unzip_sec": "3.85", "zippedMiB": "377.95" } }
{ "cfg": { "useWebWorkers": false, "level": 1 }, "results": { "zip_sec": "17.42", "unzip_sec": "3.81","zippedMiB": "377.95" } }
{ "cfg": { "useWebWorkers": true, "level": 6 }, "results": { "zip_sec": "6.77", "unzip_sec": "3.96", "zippedMiB": "375.34" } }
{ "cfg": { "useWebWorkers": false, "level": 6 }, "results": { "zip_sec": "6.88", "unzip_sec": "3.89", "zippedMiB": "375.34" } }
chrome{ "cfg": { "useWebWorkers": true, "level": 1 }, "results": { "zip_sec": "3.03", "unzip_sec": "2.09", "zippedMiB": "379.55" } }
{ "cfg": { "useWebWorkers": false, "level": 1 }, "results": { "zip_sec": "16.73", "unzip_sec": "2.42", "zippedMiB": "379.55" } }
{ "cfg": { "useWebWorkers": true, "level": 6 }, "results": { "zip_sec": "2.75", "unzip_sec": "2.15", "zippedMiB": "376.17" } }
{ "cfg": { "useWebWorkers": false, "level": 6 }, "results": { "zip_sec": "11.66", "unzip_sec": "2.19", "zippedMiB": "376.16" } }
QuestionIs there a way to use workers on nodejs to get the same speedup as on chrome ? Thanks ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
|
The First, make sure you're using the last version of zip.js (version // ...
// Import `web-worker` module
import Worker from 'web-worker';
// ...
// Make `Worker` global
globalThis.Worker = Worker;
// Use zip.js...The level 6 uses the native Regarding workers, note that Node.js is the only environment that does not offer minimal (and compatible) support for Web workers. Hopefully, this will change in the future, see https://developer.mozilla.org/en-US/docs/Web/API/Worker#browser_compatibility and nodejs/node#43583 for more info. |
Beta Was this translation helpful? Give feedback.
The
useWebWorkersissue in Node.js is related to the fact that theWorkerAPI is not supported natively. However, you can use a module likeweb-workerto circumvent this issue.First, make sure you're using the last version of zip.js (version
2.8.16) that I've just published. Then, you can simply installweb-workerfrom NPM and add the code below in your test file(s) to make theWorkerAPI visible to zip.js. You also might need to set the optionmaxWorkersto16to make sure all the CPU cores are used (not verified).The level 6 uses the native
…