8

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

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
};

Now my questions:

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

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

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

Thanks, R

TechnoCorner
  • 4,879
  • 10
  • 43
  • 81
  • Have you done any benchmarking to try and find out? That's where the proof is. – tadman May 04 '16 at 00:25
  • I haven't tried benchmarking but ill do it since you raised a good question – TechnoCorner May 04 '16 at 00:26
  • 3
    Not sure why it was marked duplicate. There's no coverage of performance on the "duplicated" link. But to answer your question, [it depends](https://kpdecker.github.io/six-speed/). If you're using ES6 `let` and `const` keywords in Node v6, then it is unequivocally slower. Some issues come up in Chrome 49 and 52 as well, but Firefox 46 and 49 seem to have no difference in performance between ES6 `let` and `const` vs `var`. – timolawl May 04 '16 at 00:35
  • 1
    I think it's safe to say that generating a new environment per iteration (`let`) requires more resources than not doing that (`var`). Whether or not that has any measurable impact on your code depends on your code and how engines implement this. – Felix Kling May 04 '16 at 00:53
  • Useful question with answers: [*“let” keyword vs “var” keyword*](http://stackoverflow.com/questions/762011/let-keyword-vs-var-keyword). – RobG May 04 '16 at 01:21
  • Many of the new features in javascript are designed to allow compiling of other languages into javascript (apparently it's to be the ["*assembly language of the web*"](http://www.hanselman.com/blog/JavaScriptIsWebAssemblyLanguageAndThatsOK.aspx)). Having keywords like *let* and *const* makes that task much easier. So unless you have a good reason to use them, stick with *var*. – RobG May 04 '16 at 01:24
  • 6
    @FelixKling: Not necessarily. When JIT is involved, it could be handled the same way C compilers typically handle scope; the stack space for lexically scoped names is still reserved on function entry and released on function exit, with the lexical scoping used for correctness checking and to allow name reuse; it's rarely necessary to explicitly clean it between uses. C++ compilers also tend to do this POD types, and it works even better in a garbage collected language without cleanup guarantees; JS can release the resources whenever it feels like it. – ShadowRanger May 04 '16 at 01:28
  • There are more differences between `var` and `let` than scoping. E.g. the Temporal dead zone or the addition as property to the global object (if defined in global scope). – a better oliver May 04 '16 at 07:36
  • Interesting mixed responses. Thanks for enlightening! – TechnoCorner May 06 '16 at 08:30
  • exact duplicate of [Is there a performance difference between 'let' and 'var'](http://stackoverflow.com/q/21467642/1048572), [why var declaration fast than let](http://stackoverflow.com/q/36847394/1048572), [Why is let slower than var in a for loop in nodejs?](http://stackoverflow.com/q/37792934/1048572) and probably more – Bergi Aug 14 '16 at 21:42

1 Answers1

6

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();
  • 1
    Looks like performance on node v8.11.0 is basically the same between var and let. Completion time varies between 420 and 480 ms, on my computer, for both var and let. – Drakinite Mar 21 '19 at 18:37