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

JS: simplify static boolean expressions #548

Open
qwerty287 opened this issue Dec 16, 2022 · 1 comment
Open

JS: simplify static boolean expressions #548

qwerty287 opened this issue Dec 16, 2022 · 1 comment

Comments

@qwerty287
Copy link

Example of code which is not optimized:

if (true) {
console.log("true")
}
if (false) {
console.log("false")
}

It is minified into !0&&console.log("true"),!1&&console.log("false") while it would be better to pre-evaluate the expression and just minify it into console.log("true") (of course only static boolean values).

Thanks!

@tdewolff
Copy link
Owner

It may seem like an easy optimization, but it gets tricky when there are variable declarations (var, function) within the if scope, e.g.:

if (false) {
    function f() {
        console.log('executing f');
    }
}
f() // should log the above message

This is also true for removing all code after a return, throw, break, or continue statement.

Let me take a stab at this next week!

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

2 participants