Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of Google chrome vs nodejs (v8)?

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?

like image 514
karthick Avatar asked Apr 01 '15 10:04

karthick


People also ask

Why does NodeJS use Chrome V8?

V8 is required for the current Node. js engine to function. In the absence of V8, it wouldn't have a JavaScript engine, and thus wouldn't be able to run JavaScript code. The V8 interface between C++ and JavaScript is used by the native code bindings that come with Node.

Does NodeJS run on Google V8 engine?

V8 is an open source JavaScript and WebAssembly engine, used in the Google Chrome web browser and in Node. js.

Is NodeJS high performance?

js is an extensively used technology to build high speed and robust applications.

Is NodeJS faster than C++?

C++ compiles directly to a machine's native code, allowing it to be one of the fastest languages in the world, if optimized. On the other hand, Node. js is detailed as "A platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications".


1 Answers

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.

like image 158
karthick Avatar answered Sep 23 '22 22:09

karthick