1

So I've been writing a game in JavaScript (not a web game, using a game engine where JavaScript just happens to be the scripting language). Unfortunately, the game engine's JavaScript engine is an ancient version of SpiderMonkey, which runs my game a bit slowly. While it's not terribly slow, it's slow enough that I decided to do a bit of optimization.

I know some basic optimizations like using local variables instead of globals (which is a good idea anyway), using prefix instead of postfix increment/decrement, counting down instead of up in loops, but what are some more good JavaScript optimizations?

5
  • 4
    Profile, profile, profile, profile, and profile your code before you start messing up anything with micro-optimizations like loop counter direction. Commented Dec 30, 2010 at 20:38
  • Since JS engines are all the rage these days, chances are that your performance problems would be solved if you used a more recent engine in your game. Is that really out of the question? Commented Dec 30, 2010 at 20:42
  • Sadly, yes, using a newer JS engine is out of the question. It's complicated, but suffice it to say, I suck at C++, which is what the engine is written in, and the SpiderMonkey version is so old that there have been API changes between it and the newer versions, which means I can't just replace the DLL and would instead have to edit the source. Commented Dec 30, 2010 at 20:57
  • Can't up vote Pointy enough. Don't optimize until you know where the slowdowns are, you can cause yourself a lot of trouble that way (in other words, premature optimization is the root of all evil). That said, this book has some good info: oreilly.com/catalog/9780596802806 Commented Dec 30, 2010 at 20:57
  • I do know where the slow downs are, at least sort of. I know what parts of the system need to be optimized. Thanks for the book link. Commented Dec 30, 2010 at 21:16

2 Answers 2

2

Instead of messing up the source code did you give a try to the Closure Compiler ? It's a compiler from javascript to javascript that does a few optimizations. Mostly are for size but the resulting js also runs often faster. No idea if optimizations are however V8-specific.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't know how your code is structured, but let's say that parts of it reside in functions or loops, which are run through frequently:

  • replace if() with ? : where possible

e.g.

if (expr) a = 1;
else a = 2;

becomes

a = expr ? 1 : 2;
  • turn a series of if()s into a switch() if possible
  • if you use substr(), substring() or slice() check which one is faster (on an embedded browser I once noticed a difference of factor 3). Keep an eye on their parameters, though!
  • avoid recalculation of values or calling the same function with the same parameters again, even if it's just a minor one
  • if you access the same element of an array over and over again, store it in a local variable
  • eval() is very slow (besides the fact that it is evil)
  • keep in mind that a JavaScript-engine is single-threaded. Nothing runs parallel, not even timers or intervals.

If the code turns out to be hard to read, write comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.