butternut icon indicating copy to clipboard operation
butternut copied to clipboard

Optimise certain if-else statements

Open Rich-Harris opened this issue 7 years ago • 0 comments

// 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.

Rich-Harris avatar May 16 '17 18:05 Rich-Harris