JavaScript中的'let'和'var'之间是否存在性能差异? [英] Is there a performance difference between 'let' and 'var' in JavaScript

查看:172
本文介绍了JavaScript中的'let'和'var'之间是否存在性能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个关键字在范围界定方面的差异已经在这里,但我想知道两者之间是否存在任何性能差异,如果是这样,是否可以忽略不计,或者它会在什么时候变得显着?

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.com 上测试后,我得到了以下结果: jsperf已经停止了一段时间;请参阅下面的替换代码。

After testing this on http://jsperf.com, 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中测试后,显示快于 var ,但仅限于与函数主范围不同的范围内。在主要范围中, var 的性能大致相同。在IE11和MS Edge中, 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.

目前 仅支持较新的浏览器,但较旧的浏览器仍然使用相对较多,通常不使用它的原因。如果你想在旧版浏览器无法正常运行的地方使用它,那么应该没有问题。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆