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
Average speed
Google Chrome - 1518.021ms
Node.js - 4 ms
Any idea about the difference execution speed?
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.
V8 is an open source JavaScript and WebAssembly engine, used in the Google Chrome web browser and in Node. js.
js is an extensively used technology to build high speed and robust applications.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With