minify icon indicating copy to clipboard operation
minify copied to clipboard

JS: simplify static boolean expressions

Open qwerty287 opened this issue 3 years ago • 1 comments

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!

qwerty287 avatar Dec 16 '22 16:12 qwerty287

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!

tdewolff avatar Dec 17 '22 12:12 tdewolff