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

Will requiring module in microjob harm performance? #35

Closed wong2 closed 5 years ago

wong2 commented 5 years ago
(async () => {
  const { start, job } = require("microjob");

  try {
    // start worker pool
    await start();

    // this function will be executed in another thread
    const res = await job(async () => {
      const module = require('some-module')
      return module.someFunction()
    });

    console.log(res);
  } catch (err) {
    console.error(err);
  }
})();

I'd like to know will the require in the job make the performance really bad?

wilk commented 5 years ago

It dependes but generally no. require is a kind of readFileSync operation so it takes the time of reading a file synchronously.

wilk commented 5 years ago

I'm closing this. Feel free to reopen it if you need more details about it.