Skip to content

Instantly share code, notes, and snippets.

@damiancipolat
Created February 26, 2020 04:21
Show Gist options
  • Save damiancipolat/c5e3a4b68e5f3e3d0f5df1c2e8a966c4 to your computer and use it in GitHub Desktop.
Save damiancipolat/c5e3a4b68e5f3e3d0f5df1c2e8a966c4 to your computer and use it in GitHub Desktop.
Array file prototype using a worker
const workerpool = require('workerpool');
const pool = workerpool.pool({maxWorkers: 7});
//Define filter prototype for be used into a worker.
const filterWorker = (values,fnStr) => values.filter(eval(fnStr));
//Declare new prototype for array filter into a worker.
Array.prototype.filter = async function(fn){
//Assign the operation in the worker.
const result = await pool.exec(filterWorker, [this,fn.toString()]);
pool.terminate();
return result;
};
const run = async ()=>{
const result = await [1,2,3,4,5,6,7,8].filter(e=>e==3);
console.log('fin',result);
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment