let vs var性能 [英] let vs var performance

查看:224
本文介绍了let vs var性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关ES6 Let关键字与现有var关键字的文章.

I've been reading about ES6 Let keyword vs existing var keyword.

我有几个问题.我知道,作用域"是let和var之间的唯一区别,但这对全局意味着什么?

I've got few questions. I understand that "scoping" is the only difference between let and var but what does it mean for the big picture?

function allyIlliterate() {
    //tuce is *not* visible out here

    for( let tuce = 0; tuce < 5; tuce++ ) {
        //tuce is only visible in here (and in the for() parentheses)
    };

    //tuce is *not* visible out here
};

function byE40() {
    //nish *is* visible out here

    for( var nish = 0; nish < 5; nish++ ) {
        //nish is visible to the whole function
    };

    //nish *is* visible out here
};

现在我的问题:

  1. 与var相比,是否拥有任何内存(/性能)优势?

  1. Does let posses any memory(/performance) advantage over var?

除了浏览器支持外,为什么我应该使用let over var的原因是什么?

Other than browser support, what are the reasons why i should be using let over var?

在我的代码工作流程中开始使用let now over var安全吗?

Is it safe to start using let now over var in my code workflow?

谢谢, R

推荐答案

let比node.js中的var慢得多.无论如何,版本为v6.3.0.有时这是戏剧性的.如果用let替换var,下面的代码要慢大约三倍.

let is much slower than var in node.js. Version v6.3.0 anyway. Sometimes this is dramatic. The code below is about three times slower if you replace var with let:

function collatz() {
    var maxsteps = 0;
    var maxval = 0;

    var x = 1;
    var n;
    var steps;

    while (x < 1000000) {
        steps = 0;
        n = x;
        while (n > 1) {
            if (n & 1)
                n = 3*n + 1;
            else
                n = n / 2;
            steps += 1;
        }
        if (steps > maxsteps) {
            maxsteps = steps;
            maxval = x;
        }
        x += 1;
    }
    console.log(maxval + ' - ' + maxsteps + ' steps');
}

collatz();

这篇关于let vs var性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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