-3

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.

12
  • 1
    cpus don't compare individual bits when comparing numbers. they compare ALL of the bits AT ONCE. You are optimizing the entirely wrong thing.
    – Marc B
    May 30, 2014 at 18:09
  • How many cases in total?
    – deviantfan
    May 30, 2014 at 18:09
  • 4
    Well, Dr. Knuth, the runtime has more efficient ways of interpreting a switch statement than even you would know.
    – Pointy
    May 30, 2014 at 18:10
  • I'd be a little surprised if modern engines didn't optimize this automatically to jump directly to the proper case, as long as it could determine that n is always a number. May 30, 2014 at 18:10
  • 1
    @Pointy: Right, that's why it would surprise me if this wasn't already optimized. Is "jump table" the correct term? May 30, 2014 at 18:12

2 Answers 2

1

Use an array

myCommand[n]

with command objects as elements.

1
  • 1
    You've verified this to be faster? Not saying it isn't, but since JS Arrays are objects, they can be highly optimized under certain circumstances, and not under others. And then function calls require overhead as well (though the OP may be invoking functions in a similar manner, can't tell). May 30, 2014 at 18:44
-1

JavaScript is interpreted, so any optimization you attempt to do will be slower than the optimizations of the JavaScript interpreter, which will be compiled and run faster.

Just don't worry about it.

5
  • 1
    This is wrong in general. Please explain your answer.
    – djechlin
    May 30, 2014 at 18:14
  • I'm not understanding the "JavaScript is interpreted, so..." part. How does it being interpreted help? May 30, 2014 at 18:15
  • @cookiemonster Because interpreted languages usually run slower than compiled ones
    – Oriol
    May 30, 2014 at 18:16
  • Oh I see what you're saying, but I don't know if I necessarily agree with such a broad statement. After all, the switch statement is interpreted too. I think the OP is asking if there's any construct or approach to the problem that would have better optimization than a switch. May 30, 2014 at 18:20
  • 1
    I don't think this answer was phrased optimally, but it's really a great point - micro-optimizations in your JavaScript source code might help today, but tomorrow some whiz working on the V8 code might have a great idea that makes your hacks irrelevant or actually slower than straightforward code. Joe Armstrong, the Erlang guy, always tells people to just "write the most beautiful code you can". Slow code is almost always an algorithmic problem anyway.
    – Pointy
    May 30, 2014 at 19:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.