简体   繁体   English

Google Chrome与nodejs(v8)的表现如何?

[英]Performance of Google chrome vs nodejs (v8)?

在此输入图像描述

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 . 上述代码在nodejs运行fastergoogle chrome faster Why node.js is faster than google chrome both are using chrome v8 engine 为什么node.js比谷歌chrome更快都使用chrome v8引擎

Note 注意

Average speed 平均速度

 Google Chrome  - 1518.021ms 

 Node.js - 4 ms

Any idea about the difference execution speed? 关于差异执行速度的任何想法?

In a web browser(Chrome), declaring the variable i outside of any function scope makes it global and therefore binds to window object. 在Web浏览器(Chrome)中,在任何函数范围之外声明变量i使其成为全局变量并因此绑定到window对象。 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. 因此,在Web浏览器中运行此代码需要在for循环的每次迭代中重复解析人口密集的窗口命名空间内的属性。

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. 但是,在Node.js中,声明任何函数范围之外的任何变量只会将其绑定到模块范围(而不是窗口对象),因此可以更容易,更快地解析。

We will get more or less same execution speed when we wrap the above code in function. 当我们在函数中包含上面的代码时,我们将获得或多或少相同的执行速度。

Its Due To SCOPE in JavaScript 归功于 JavaScript中的SCOPE

In Browser console code without scope : Takes lots of time : Test: 1154.19ms 没有范围的 浏览器控制台代码中 :花费 大量时间: 测试: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 具有范围的 浏览器控制台代码中 :花费 更少的时间测试:3.06ms

below code will be kept inside JavaScript scope and scope will be almost empty so less time 下面的代码将保存在JavaScript范围内, 范围几乎为空,因此时间更短

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 Nodejs REPL中 :没有范围的代码: 测试:14ms

Unexpected But nodejs most outer scope contains some bunch of variables 意外但是nodejs大多数外部范围包含一些变量

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

In Nodejs REPL : code with scope : Test: 2ms Nodejs中REPL :具有范围的代码: 测试:2ms

below code will be kept inside JavaScript scope and scope will be almost empty so less time 下面的代码将保存在JavaScript范围内, 范围几乎为空,因此时间更短

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 结论:由于JavaScript中的SCOPESCOPING都是由函数完成的

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 意味着如果你创建新函数, 花括号{}内的位置被称为SCOPE ,它最初将为空,如果你在这个范围内创建变量,它将花费时间执行

It's far from being as dramatic now, but I did get 2x boost from 5.417ms down to 2ms on your test. 它现在远没有那么引人注目,但我确实从你的测试中的5.417毫秒下降到2毫秒。 I had near absolute same values on Node and Chrome when I used a larger loop and a function wrap. 当我使用更大的循环和函数换行时,我在Node和Chrome上具有接近绝对相同的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM