Google Chrome与Node.js(v8)的性能? [英] Performance of Google chrome vs nodejs (v8)?
问题描述
console.time("Test");
for(var i=0; i <2500000; i +=1 ){
// loop around
}
console.timeEnd("Test");
上面的代码在nodejs
中比google chrome
在faster
中运行.为什么node.js比Google chrome都快使用chrome v8
引擎
The above code runs faster
in nodejs
than google chrome
. Why node.js is faster than google chrome both are using chrome v8
engine
平均速度
Google Chrome - 1518.021ms
Node.js - 4 ms
对差异执行速度有任何想法吗?
Any idea about the difference execution speed?
推荐答案
在网络浏览器(Chrome)中,在任何函数作用域之外声明变量i使其成为全局变量,因此绑定到 window 对象.因此,在Web浏览器中运行此代码需要在for循环的每次迭代中重复解决人口稠密的窗口命名空间中的属性.
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.
但是,在 Node.js 中,声明任何函数范围之外的任何变量只会将其绑定到 module 范围(而不是窗口"对象),因此更加容易更快地解决.
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.
这篇关于Google Chrome与Node.js(v8)的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!