wilk / microjob

A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
https://wilk.github.io/microjob/
MIT License
2.02k stars 47 forks source link

Suggestion: CPU bound example #23

Closed larshp closed 5 years ago

larshp commented 6 years ago

Hi, The Sync example runs in a different thread as described, and I did not succeed getting the async example working. However, it looks like both examples are not limited by CPU, suggest adding an example which utilites all cores.

Following is what I've come up with:

let res = [];
for (let j = 0; j < 10000; j++) {
  res.push(job(() => {
    let total = 0;
    for (let i = 0; i < 10000; i++) {
      total = total + Math.floor(Math.random() * Math.floor(100));
    }
    return total;
  }));
}
console.log(await Promise.all(res));
wilk commented 6 years ago

The main example is intended to show how microjob works: just define a function with a heavy CPU load so it will be executed in a separated thread. This is the purpose of microjob: simplicity. Your example could be useful to show how the underneath worker pool works.

Instead, I suggest to create an examples folder, both for JS and TS, full of useful examples.

wilk commented 5 years ago

I've added the examples folder: I consider this issue closed. Feel free to re-open again if you think it does not satisfy your type of CPU bound examples.