Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A better for-loop

22 views
Skip to first unread message

Jason Orendorff

unread,
Feb 2, 2012, 1:28:39 PM2/2/12
to dev-tech-js-en...@lists.mozilla.org
This is so neat I have to share...

Brendan tweeted that TC39 is considering making this code:

for (let i = 0; i < 10; i++)
setTimeout(function () { alert(i); }, 0);

work as expected, alerting each digit in turn, instead of alerting "10"
ten times.

I think this is really easy to implement in SpiderMonkey! Currently we
emit bytecode like this:

for (let V = INIT; TEST; UPD) STMT

INIT
enterlet V
goto L2
L1: STMT
UPD
L2: TEST
iftrue L1
leaveblock

The new way only requires one tiny change: add a "reenterblock"
instruction between STMT and UPD! The new instruction would just exit
the current block, then do the exact same thing as "enterlet V". All the
code already exists.

-j

Wes Garland

unread,
Feb 2, 2012, 1:33:43 PM2/2/12
to Jason Orendorff, dev-tech-js-en...@lists.mozilla.org
\o/
0 new messages