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
Optimize a[n]; n++ inside loopings #8710
Labels
Milestone
Comments
Here is a bad case: const a = [1, 2, 3];
let n = 0;
const x = (i) => {
if(n !== 0) {
console.log("---");
}
console.log(i);
}
for (; n < a.length; )
x(a[n]), n++; Result:
"minified": const a = [1, 2, 3];
let n = 0;
const x = (i) => {
if(n !== 0) {
console.log("---");
}
console.log(i);
}
for (; n < a.length; )
x(a[n++]); Result:
|
You are right! Maybe it can check if function has scope greater then the variable. |
You also need to take account exceptions/try..catch statements: function x(a: number) {
throw "Error";
}
let n = 5;
try {
x(n);
n++;
} catch { }
console.log(n); Output: function x(a: number) {
throw "Error";
}
let n = 5;
try {
x(n++);
} catch { }
console.log(n); Output: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the feature
In cases where we manipulate a number using
++
or--
after its use, it may be possible to use it inline, reducing a few bytes (~2 bytes per use). Currently, this feature works outside of looping.For instance:
Will be (prettified):
Could be:
So
n
will be post-incremented, but it will return the old value to access the index ata
anyway.It is possible to think of other variations to this rule, although most cases will be simple like the first example.
Note: currently Terser does not support this kind of optimization.
Babel plugin or link to the feature description
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: