Tailwind and Elm = Love?

Ah, yes, minifiers do some analysis to detect constants and remove dead branches from if and switch (and more). That’s true!

But sticking that into a function is enough to fool Terser:

function toDayString(int) {
  switch (int) {
    case 0:
      return "Sunday";
    case 1:
      return "Monday";
    case 2:
       return "Tuesday";
    case 3:
      return "Wednesday";
    case 4:
      return "Thursday";
    case 5:
      return "Friday";
    case 6:
      return "Saturday";
  }
}

toDayString(0);

So it’s not safe to assume that unused branches are removed always/in general/most of the time?

Replacing the last line by

console.log(toDayString(0));

and using the command-line-tool gives the expected result:

$ terser --toplevel --compress 'passes=2' switch.js 
console.log("Sunday");

Terser needs more passes (and time) and the --toplevel option to ensure, that the function is not needed outside this code. But it’s again easy to fool, e.g by

console.log(toDayString(0) + " " + toDayString(6));

I had expected more. Sorry for getting off topic.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.