0

I have in my application a calculation like

    var points= [...3000];
    for (var i = 0; i < points.length; i++) {
      for (var j = i+1; j < points.length-1; j++) {
        for (var k = k+1; k < points.length; k++) {}
      }
    }

I use a 2 core processor and 2GB of RAM and the calculation is solved in 97132ms I did an upgrade on my server and I'm using a 4 core and 8GB of RAM, but i got the same result after upgrade

I try

if (cluster.isMaster) {
    var numCPUs = os.cpus().length;

    // Master:
    // Let's fork as many workers as you have CPU cores

    for (var i = 0; i < numCPUs; ++i) {
        cluster.fork();
    }

and execute my application like node --nouse-idle-notification --expose-gc --stack_size=7168 --max-old-space-size=7168 bin/www

I think de node v8 doesn't use 100% from my CPU

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
FeeFez
  • 1
  • 2

1 Answers1

2

node.js by itself runs your Javascript in a single thead and only uses one core for running it. So, if your Javascript is compute bound, it will only use one core and the performance will not fundamentally differ for a 2, 4 or 8 core computer.

Some modules written in native code may use native threads for some of their work (which may bring other cores into play), but this does not apply to the execution of your own Javascript.

Long running CPU-bound calculations can also be spawned into another process which can communicate back it's results. When you fire up another process, you are creating the opportunity for the system to use a different core for that other process.

Clustering creates the opportunity to run multiple independent copies of your process that can each carry out their own separate jobs. But, if you're timing one particular job, that job is going to be executed in just one of the clusters and thus use only one of the CPUs.

node.js does not by itself use multiple CPUs to speed up one Javascript execution task.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • JavaScript code is executed by means of the V8 and it probably uses threads internally. Even though js is single threaded, this doesn't apply to the surrounding engine. Am I wrong? – skypjack Jan 24 '16 at 19:02
  • @skypjack: V8 is single threaded internally except for disk I/O – slebetman Jan 24 '16 at 19:04
  • Interesting. Is it documented somewhere? I'd like to better understand how it works internally. – skypjack Jan 24 '16 at 19:06
  • @skypjack - Its all about the event loop architecture that node.js (and JS in the browser) use. You can read about that architecture in [this node.js specific article](https://nodesource.com/blog/understanding-the-nodejs-event-loop/) and [this more detailed article](http://www.journaldev.com/7462/node-js-processing-model-single-threaded-model-with-event-loop-architecture) and [this post](http://stackoverflow.com/questions/7575589/how-does-javascript-handle-ajax-responses-in-the-background/7575649#7575649) which also contains 8 other referenced articles. – jfriend00 Jan 24 '16 at 19:10
  • Wait a moment: event loop architecture and js engine are not the same, you are mixing up libuv and v8 in your comment... – skypjack Jan 24 '16 at 19:12
  • 1
    @skypjack - I'm not aware of any references that discuss only the internal architecture of the V8 JS execution engine and not the overall evented I/O architecture. There would be opportunities for V8 to use threads in some things (like parsing/compiling separate blocks of Javascript). slebetman seems to think it does not use threads for even that kind of stuff - I have no idea. I do know that once it is executing Javascript code, that is done with only one thread which is the part that is most relevant to the OP's question. – jfriend00 Jan 24 '16 at 19:39
  • @jfriend00 Yeah, absolutely, I got the chance to ask because you touched the topic. Thanks for your answer anyway. – skypjack Jan 24 '16 at 22:33
  • @slebetman, at the very least v8 uses a separate thread for background compilation blog.chromium.org/2014/02/compiling-in-background-for-smoother.html – Vladislav Ivanishin Jan 25 '16 at 06:05