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

Way to reduce +=1 to ++ in for loops? #3280

Open
jamessouth opened this issue Oct 18, 2018 · 7 comments
Open

Way to reduce +=1 to ++ in for loops? #3280

jamessouth opened this issue Oct 18, 2018 · 7 comments

Comments

@jamessouth
Copy link

Bug report or feature request?

feature req

Uglify version (uglifyjs -V)

3.4.9

JavaScript input

for(var i = 0; i < 3; i += 1){
  console.log(i);
}

The uglifyjs CLI command executed or minify() options used.

uglifyjs [filename.js]

JavaScript output or error produced.

for(var i=0;i<3;i+=1){console.log(i)}

maybe this is a source code issue and outside the scope of minification, just wondering if there was/could be an option to turn +=1 and -=1 to ++ and -- ?

@fabiosantoscode
Copy link
Contributor

Added the feature to terser, under the existing "loops" option.

@fabiosantoscode
Copy link
Contributor

Released as terser 3.10.8

@fabiosantoscode
Copy link
Contributor

You might want to pin that version, as I had to revert the feature due to issues. I might try to do it again sometime, but for now it's out.

@Skalman
Copy link
Contributor

Skalman commented Nov 6, 2018

It's very tricky to do correctly, because when the variable is a string it change the meaning:

// 0, 1, 2
for(var i = 0; i < 3; i += 1){console.log(i)}

// "0", "01"
for(var i = "0"; i < 3; i += 1){console.log(i)} //

@drathier
Copy link

drathier commented Nov 8, 2020

Maybe an "assume no implicit type coercion anywhere" optimization flag could be useful?

@Skalman
Copy link
Contributor

Skalman commented Nov 8, 2020

@drathier, you'd have to specify it much more clearly than that. E.g. I agree that "hello" + 123 is implicit, but "" + 123 is an explicit conversion - that's the easiest way to convert a number to a string.

@drathier
Copy link

drathier commented Nov 9, 2020

"hello" + 123 is just as explicit as "" + 123. What I meant was that n + 123 could assume that n is a number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants