Web Workers vs. Child_process for CPU Intensive Functions in Node.js

I am trying to use node-unfluff , which extracts content from HTML strings. However, startup typically requires ~ 200 ms. Since it works synchronously, it is too slow. I want to run it asynchronously.

As far as I know, my options are web workers ( https://github.com/audreyt/node-webworker-threads ) or child_process ( https://nodejs.org/api/child_process.html ). Are there any other options?

If not, which one is better in terms of speed or other factors?

Edit:

There are also à gogo themes ( https://github.com/xk/node-threads-a-gogo ) and a tiny worker ( https://github.com/avoidwork/tiny-worker ).

WebWorker themes do not support require , so this is no longer an option.

This is possible for require files using Threads à gogo using the load function, but it seems like a hacky workaround.

the tiny employee only has 26 stars on Github, so I hesitate to use it in production code. It supports require .

I am considering writing my own implementation of WebWorker using child_process if there are no better options.

+6
source share
1 answer

You can use require with Workers. In your working script you need to call

 self.importScripts('../path/require.js'); 

At the request of the documents, you can transfer the configuration object to the module:

 requirejs.config({ //By default load any module IDs from js/lib baseUrl: 'js/lib', //except, if the module ID starts with "app", //load it from the js/app directory. paths //config is relative to the baseUrl, and //never includes a ".js" extension since //the paths config could be for a directory. paths: { app: '../app' } }); // Start the main app logic. requirejs(['jquery', 'canvas', 'app/sub'], function ($, canvas, sub) { //jQuery, canvas and the app/sub module are all //loaded and can be used here now. }); 

Combination

worker.js

 self.importScripts('../path/require.js'); requirejs.config({ //By default load any module IDs from path/lib baseUrl: 'path/lib', //except, if the module ID starts with "app", //load it from the js/app directory. paths //config is relative to the baseUrl, and //never includes a ".js" extension since //the paths config could be for a directory. paths: { app: '../app' } }); // Start the main app logic. requirejs(['jquery', 'canvas', 'app/sub'], function ($, canvas, sub) { //jQuery, canvas and the app/sub module are all //loaded and can be used here now. // now you can post a message back to your callee script to let it know require has loaded self.postMessage("initialized"); }); self.onmessage = function(message) { // do cpu intensive work here, this example is not cpu intensive... if(message.data === 'to process') { self.postMessage("completed!"); } } 

Node Work Challenge

 var worker = new Worker('Worker.js'); worker.onmessage = function(event) { var msg = event.data; if(msg === 'initialized') { worker.postMessage({data: 'to process'}); } } 
+1
source

Source: https://habr.com/ru/post/1014398/


All Articles