Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lift var declarations to the loop initializer #290

Closed
kangax opened this issue Nov 22, 2016 · 4 comments
Closed

Lift var declarations to the loop initializer #290

kangax opened this issue Nov 22, 2016 · 4 comments

Comments

@kangax
Copy link
Member

kangax commented Nov 22, 2016

Currently, this code:

for (var i = 0; i < 0; i++) {
  var j = jj();
  console.log(j);
}

transforms to:

for(var i=0;0>i;i++){var j=jj();console.log(j)}

but if we lift var declarations to the loop, we can get it down to:

for(var i=0,j;0>i;i++){j=jj();console.log(j)}

This becomes irrelevant with multiple declarations so we might only consider doing this in certain cases but not others:

for (var i = 0; i < 0; i++) {
  var j = jj(), k = kk(), l = ll();
  console.log(j, k, l);
}

transforms to:

for(var i=0;0>i;i++){var j=jj(),k=kk(),l=ll();console.log(j,k,l)}

and is same length with lifted:

for(var i=0,j,k,l;0>i;i++){j=jj(),k=kk(),l=ll();console.log(j,k,l)}
@boopathi
Copy link
Member

boopathi commented Nov 22, 2016

for(var i=0;;){var j=foo;}
for(var i=0,j;;){j=foo;}

for(var i=0;;){var j=foo,k=bar;}
for(var i=0,j,k;;){j=foo,k=bar;}

// and we can stop at 2 or even 1
for(var i=0;;){var j=foo,k=bar,l=baz;}
for(var i=0,j,k,l;;){j=foo,k=bar,l=baz;}

The gotcha I see here is the order of transformations.

for (var i = 0;;) { var j = foo; var k = bar; }

merging sibling variables vs merging to previous for-loop.

@boopathi
Copy link
Member

We can do this as a part of merging-sibling-variables only ... and not worry about order. !!!

@vigneshshanmugam
Copy link
Member

vigneshshanmugam commented Nov 23, 2016

@kangax @boopathi I can work on this if you guys are okay with it :)

@kangax
Copy link
Member Author

kangax commented Nov 23, 2016

@vigneshshanmugam sure, any help is welcome!

@kangax kangax closed this as completed in aeba4e6 Nov 27, 2016
kangax added a commit that referenced this issue Nov 27, 2016
lift var declarations to initializer (fix #290)
boopathi pushed a commit that referenced this issue Nov 27, 2016
fix a empty loop scenario

add more tests and fixes

handle for statments without block

lift both var/let declarations

move function out of ForStatment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants