Rich-Harris / butternut

The fast, future-friendly minifier
https://butternut.now.sh
MIT License
1.17k stars 17 forks source link

Optimise certain if-else statements #106

Open Rich-Harris opened 7 years ago

Rich-Harris commented 7 years ago
// input
if ( x ) {
  foo = 1;
} else {
  foo = 2
}

// output
x?(foo=1):(foo=2)

// better output
foo=x?1:2
// input
if ( x ) {
  return 1;
} else {
  return 2;
}

// output
if(x){return 1}else{return 2}

// better output
return x?1:2

There may be some more general rule that captures these specific examples, but having a special case for them wouldn't be the worst thing in the world.