Performance impact of let only code

163 views
Skip to first unread message

Trevor Norris

unread,
Mar 11, 2015, 5:17:52 PM3/11/15
to streng...@googlegroups.com
There is one specific case where use of let has a noticeable performance impact, and where the work around, instead of just using var, is not pretty. Example:

'use strict';
const ITER = 1e7;


let t
= performance.now();

// Use of "let" in for loop.
for (let i = 0; i < ITER; i++);
t
= performance.now() - t;
print(t);


t
= performance.now();
// Use of "var" in for loop.
for (var i = 0; i < ITER; i++);
t
= performance.now() - t;
print(t);


// Output:
// 486.324
// 28.25


Those results are for d8 4.2.77.6 on a x64.release Linux build. To work around this I need to hoist the variable declaration, or to preserve scoping need to do something ridiculous like so:

{
  let i
= 0;
 
for (; i < ITER; i++);
}


The point is, if var is going to be completely removed then use of let in tight for loops like what's shown above need to get faster.

Dmitry Lomov

unread,
Mar 11, 2015, 5:29:57 PM3/11/15
to Trevor Norris, streng...@googlegroups.com
Thanks Trevor, I am working as we speak to make this faster.
The dirty secret is that in current V8 all 'let' and 'const' variables are actually context-allocated, not stack-allocated.
This will be fixed very soon though.

Cheers,
Dmitry

--
You received this message because you are subscribed to the Google Groups "Strengthen JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strengthen-j...@googlegroups.com.
To post to this group, send email to streng...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/strengthen-js/80ab62ae-9116-4c19-a16b-6c6540bfae85%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Trevor Norris

unread,
Mar 11, 2015, 5:31:53 PM3/11/15
to streng...@googlegroups.com, trev....@gmail.com
On Wednesday, March 11, 2015 at 3:29:57 PM UTC-6, Dmitry Lomov wrote:
Thanks Trevor, I am working as we speak to make this faster.
The dirty secret is that in current V8 all 'let' and 'const' variables are actually context-allocated, not stack-allocated.
This will be fixed very soon though.

That's great to hear. Thanks for letting me know.

Si Robertson

unread,
Mar 14, 2015, 4:07:46 PM3/14/15
to streng...@googlegroups.com, trev....@gmail.com
The dirty secret is that in current V8 all 'let' and 'const' variables are actually context-allocated, not stack-allocated.

I don't suppose you know of a bug report that we can keep an eye on? I searched through the V8 issues but failed to find anything related to this.

Trevor Norris

unread,
Apr 20, 2015, 3:24:10 PM4/20/15
to streng...@googlegroups.com, trev....@gmail.com
On Wednesday, March 11, 2015 at 3:29:57 PM UTC-6, Dmitry Lomov wrote:
Thanks Trevor, I am working as we speak to make this faster.
The dirty secret is that in current V8 all 'let' and 'const' variables are actually context-allocated, not stack-allocated.
This will be fixed very soon though.

Hey Dmitry. Just wanted to check in on this. I just ran a test on 4.4.26 and there still seems to be a noticeable performance impact between

for (let i = 0; i < 1e7; i++) { }

and

let i = 0;
for (; i < 1e7; i++) { }

Thanks!

Dmitry Lomov

unread,
Apr 20, 2015, 4:42:26 PM4/20/15
to Trevor Norris, streng...@googlegroups.com
This is the patch: https://codereview.chromium.org/981203003/ - not yet landed.
(Debugger changes in particular turned out to be quite involved). 

--
You received this message because you are subscribed to the Google Groups "Strengthen JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strengthen-j...@googlegroups.com.
To post to this group, send email to streng...@googlegroups.com.

Dmitry Lomov

unread,
Apr 22, 2015, 11:38:10 AM4/22/15
to Trevor Norris, streng...@googlegroups.com
The fix has landed, and Trevor's test shows similar performance (let variation is actually a bit faster)
Reply all
Reply to author
Forward
0 new messages