So, I have a piece of Javascript code that's like
switch (n)
{
case 1:
// ...
case 2:
// ...
case 3:
// ...
case 4:
// ...
default:
// never happens
}
However, I realize there's a problem because there will be a redundancy in checking n
against 1
and 3
because if the first bit of 1
is off then 3
doesn't even need to be checked; likewise, if the first bit of 2
is on then 4
doesn't even need to be checked. How can I optimize this procedure? I need fast code because this logic is part of a game that runs very fast.
switch
statement than even you would know.n
is always a number.