Created
February 26, 2020 04:21
-
-
Save damiancipolat/c5e3a4b68e5f3e3d0f5df1c2e8a966c4 to your computer and use it in GitHub Desktop.
Array file prototype using a worker
This file contains 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 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