问题描述
这两个关键字在范围方面的区别已经在这里进行了彻底讨论,但我想知道两者之间是否存在任何类型的性能差异,如果有,是否可以忽略不计,或者在什么时候会变得显着?
The difference between these two keywords in terms of scoping has already been thoroughly discussed here, but I was wondering if there is any kind of performance difference between the two, and if so, is it negligible, or at what point would it become significant?
推荐答案
在 http://jsperf 上测试后,我得到如下结果: jsperf 已经down了一段时间了;请参阅下面的替换代码.
After testing this on http://jsperf, I got the following results: jsperf has been down for a while; see the replacing code below.
为了检查这一点,我将使用基于这个答案的以下性能测试,这促使我写了这个功能:
To check this, I'll use the following performance test based on this answer, which led me to write this function:
/**
* Finds the performance for a given function
* function fn the function to be executed
* int n the amount of times to repeat
* return array [time for n iterations, average execution frequency (executions per second)]
*/
function getPerf(fn, n) {
var t0, t1;
t0 = performance.now();
for (var i = 0; i < n; i++) {
fn(i)
}
t1 = performance.now();
return [parseFloat((t1 - t0).toFixed(3)), parseFloat((repeat * 1000 / (t1 - t0)).toFixed(3))];
}
var repeat = 100000000;
var msg = '';
//-------inside a scope------------
var letperf1 = getPerf(function(i) {
if (true) {
let a = i;
}
}, repeat);
msg += '<code>let</code> inside an if() takes ' + letperf1[0] + ' ms for ' + repeat + ' iterations (' + letperf1[1] + ' per sec).<br>'
var varperf1 = getPerf(function(i) {
if (true) {
var a = i;
}
}, repeat);
msg += '<code>var</code> inside an if() takes ' + varperf1[0] + ' ms for ' + repeat + ' iterations (' + varperf1[1] + ' per sec).<br>'
//-------outside a scope-----------
var letperf2 = getPerf(function(i) {
if (true) {}
let a = i;
}, repeat);
msg += '<code>let</code> outside an if() takes ' + letperf2[0] + ' ms for ' + repeat + ' iterations (' + letperf2[1] + ' per sec).<br>'
var varperf2 = getPerf(function(i) {
if (true) {}
var a = i;
}, repeat);
msg += '<code>var</code> outside an if() takes ' + varperf1[0] + ' ms for ' + repeat + ' iterations (' + varperf1[1] + ' per sec).<br>'
document.getElementById('out').innerHTML = msg
<output id="out" style="font-family: monospace;white-space: pre-wrap;"></output>
在 Chrome 和 Firefox 中对此进行测试后,这表明 let
比 var
更快,但前提是在与函数的主作用域不同的作用域内.在主要范围内,var
和 let
在性能上大致相同.在 IE11 和 MS Edge 中,let
和 var
在两种情况下的性能大致相同.
After testing this in Chrome and Firefox, this shows that let
is faster than var
, but only when inside a different scope than the main scope of a function. In the main scope, var
and let
are roughly identical in performance. In IE11 and MS Edge, let
and var
are roughly equal in performance in both cases.
按下蓝色大按钮,在您喜欢的浏览器中亲自查看.
Press the big blue button to see for yourself in your favourite browser.
目前let
仅支持较新的浏览器,但较旧的浏览器还在使用比较多,这将是一个普遍不使用它的原因.如果您想在旧浏览器无法正常运行的地方使用它,应该没有问题.
Currently let
has support from only newer browsers, but older browsers are still being used relatively much, which would be a reason to generally not use it yet. If you want to use it somewhere where older browsers wouldn't function otherwise, there should be no problem with it.
修改了答案,因为 jsperf 不起作用(请参阅旧版本的修订历史记录).
revamped answer since jsperf is not working (see revision history for old version).
这篇关于JavaScript 中的“let"和“var"之间是否存在性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论