17

enter image description here

Example

     console.time("Test");
     for(var i=0; i <2500000; i +=1 ){
             // loop around
     }
     console.timeEnd("Test");

The above code runs faster in nodejs than google chrome. Why node.js is faster than google chrome both are using chrome v8 engine

Note

Average speed

 Google Chrome  - 1518.021ms 

 Node.js - 4 ms

Any idea about the difference execution speed?

3
  • How are you doing these performance tests ? How do you run the javascript in Chrome? Commented Apr 1, 2015 at 10:20
  • In your simple example the loop ultimately does nothing but change the variable i from 0 to several million. The V8 engine's JIT compiler will optimize parts like this and may not even run the loop at all if it can get the same result. As you can see, this will depend on the environment the JS is run in. This is a relevant JS talk on performance and benchmarking: youtube.com/watch?v=65-RbBwZQdU Commented Jun 9, 2017 at 2:01
  • But when I am trying the below code in nodejs and chrome, then chrome is taking 522 ms and nodejs is taking 2277 ms. It means chrome is faster. function test(){ const startTime = new Date().getTime(); for(var i=0;i<10000;i++){ console.log(i); } const endTime = new Date().getTime(); console.log('Execution time::' + (endTime - startTime )); // in nodejs Execution time::2277 and in chrome Execution time::522 } test(); Commented Jun 4, 2019 at 13:39

3 Answers 3

30

In a web browser(Chrome), declaring the variable i outside of any function scope makes it global and therefore binds to window object. As a result, running this code in a web browser requires repeatedly resolving the property within the heavily populated window namespace in each iteration of the for loop.

In Node.js however, declaring any variable outside of any function’s scope binds it only to the module scope (not the window object) which therefore makes it much easier and faster to resolve.

We will get more or less same execution speed when we wrap the above code in function.

Sign up to request clarification or add additional context in comments.

1 Comment

Also note how you get a significant speed-up by using let i instead of var i, which makes it a local variable.
6

Its Due To SCOPE in JavaScript

In Browser console code without scope : Takes lots of time : Test: 1154.19ms

below code will be kept in heavily populated window object which will take time to resolve;

 console.time("Test");
 for(var i=0; i <2500000; i +=1 ){
         // loop around
 }
 console.timeEnd("Test");

In Browser console code with scope : Takes less time Test: 3.06ms

below code will be kept inside JavaScript scope and scope will be almost empty so less time

function rocket(){
    console.time("Test");
     for(var i=0; i <2500000; i +=1 ){
             // loop around
     }
     console.timeEnd("Test");
}
rocket()

In Nodejs REPL : code without scope : Test: 14ms

Unexpected But nodejs most outer scope contains some bunch of variables

 console.time("Test");
 for(var i=0; i <2500000; i +=1 ){
         // loop around
 }
 console.timeEnd("Test");

In Nodejs REPL : code with scope : Test: 2ms

below code will be kept inside JavaScript scope and scope will be almost empty so less time

function rocket(){
    console.time("Test");
     for(var i=0; i <2500000; i +=1 ){
             // loop around
     }
     console.timeEnd("Test");
}
rocket()

Conclusion : Its all due to SCOPE and SCOPING in JavaScript is done by functions

Means if you create new function ,the place inside curly brackets {} is called as SCOPE and it will be empty initially, and if you create variable in this scope it will take no time to execute

Comments

1

It's far from being as dramatic now, but I did get 2x boost from 5.417ms down to 2ms on your test. I had near absolute same values on Node and Chrome when I used a larger loop and a function wrap.

(function(){
console.time("Test");
for(var i=0; i <10000000000; i +=1 ){
     // loop around
}
console.timeEnd("Test");
}).call(this);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.