Performance tips for JavaScript Game Developers

Development

Introduction

This post originally started out as a comment to this thread, but the content ended up being too large to post as a comment, so I decided to post it as it's own thread.

This post details all of the performance tips I've been exposed to over my years as a software developer working primarily in JavaScript and on the web.

I'll just get straight into it...

Cache your DOM selectors

This is a very low-hanging fruit with a low impact. It doesn't do much when you have just a few elements, but it makes a noticeable difference when there are 1000s of elements on the page.

Don't do this:

document.addEventListener("click", event => {
  const myElement = document.querySelector("#foo");
  // do something with `myElement`
});

Do this instead:

const myElement = document.querySelector("#foo");

document.addEventListener("click", event => {
  // do something with `myElement`
});

Find child elements based on a common ancestor

When you use code like the following, you are checking the entire document for a particular element.

const someParentElement = document.querySelector(".parent");
const someChildElement = document.querySelector(".child");

If you are looking for an element that you know is inside another element, and you already have that element's reference cached somewhere, you can limit the amount of elements the browser needs to compare to your query to just those elements inside the parent, like so:

const someParentElement = document.querySelector(".parent");
const someChildElement = someParentElement.querySelector(".child");

If you do not have a parent element reference, but you do know that the element you're looking for is at the same level of heirarchy as another element which you do have a reference for, then you can jump up a level to the common parent like so:

const sharedParent = siblingElement1.parentNode;
const siblingElement2 = sharedParent.querySelector(".hello");

Favor requestAnimationFrame over timers

Animation frames are generally far more performant than timers that simulate repaints.

Don't do this (this supposedly runs 60 frames per second):

setInterval(function gameLoop() {
  // game render code here
}, 1000 / 60);

Do this instead:

function gameLoop(timestamp) {
  // game render code here
  
  requestAnimationFrame(gameLoop);
}

requestAnimationFrame(gameLoop);

This has the added benefit of running at a guaranteed 60+ fps. Higher monitor refreshrates actually run this at a higher frame rate, for example my ASUS VG248QE will run this game loop at 144 fps. Just bare in mind that in order to save system resources, browsers will "pause" the execution of an animation frame if focus is lost. This can happen when the user tabs out, minimizes the window or idles for a few minutes. Be sure to accommodate this behavior in your calculations, for example you might want to check how much time has passed since the last "tick" on each new "tick", and calculate your player's profits based on that.

Do not rely on time calculations derived from the `Date` object

There is a far more precise time API accurate down to microseconds - look at Performance.now().

Don't do this:

const timeStarted = new Date();
function gameLoop(timestamp) {
  const currentTime = new Date();
  const timeElapsed = currentTime - timeStarted;
  
  requestAnimationFrame(gameLoop);
}

requestAnimationFrame(gameLoop);

Do this instead:

function gameLoop(timestamp) {
  const timeElapsed = performance.now();
  
  requestAnimationFrame(gameLoop);
}

requestAnimationFrame(gameLoop);

Or better yet, use the timestamp passed to the requestAnimationFrame callback - it is the same high-precision time object type as performance.now\`()`, but the origin of the time starts when rAF first begins executing callbacks:

function gameLoop(timestamp) {
  const timeElapsed = timestamp;
  
  requestAnimationFrame(gameLoop);
}

requestAnimationFrame(gameLoop);

Of course, if you actually need to use dates - for example calculating per day numbers or just rendering a calendar or whatever, then by all means use the Date object.

Always measure your FPS

You won't know when performance tanks if you don't know what numbers to reasonably expect. Most browser developer consoles have a function to show an FPS counter on the page. In Chrome, it's in developer tools -> command palette (ctrl+p on win, cmd+p on mac) -> show frames. This will show you frames per second rendering for the entire page, but if you want to measure the FPS of a single game loop, say, for a single canvas element, then you can derive that from the timestamp parameter:

const fpsEl = document.getElementById("fps");

let secondsPassed;
let oldTimestamp;
let fps;

function gameLoop(timestamp) {
  secondsPassed = (timestamp - oldTimestamp) / 1000;
  oldTimestamp = timestamp;
  
  fps = Math.round(1 / secondsPassed);

  fpsEl.textContent = fps;

  requestAnimationFrame(gameLoop);
}

requestAnimationFrame(gameLoop);

Routinely generate and measure CPU profiles and heapmaps

Let's say you encounter a performance issue. How do you begin to analyze it, find out where it's coming from, all that nonsense? It's not as simple as looking at your game and thinking, "Well, it happened when I jumped, so obviously it's my jump code."

What you can do instead, is record a performance profile. You can then generate a heapmap from that profile, and jump into the callstack to see exactly how many milliseconds were spent running particular functions and methods.

How is this done?

It's quite an involved process, with far too much information for me to condense into a section of a Reddit comment/post, so unfortunately you will have to click on a link for this one. Here you go: https://blog.appsignal.com/2020/02/20/effective-profiling-in-google-chrome.html

Execute DOM Reads and Writes in different phases

Did you know that most browsers optimise for the case when reads and writes exist in separate animation frames/phases? Let me demonstrate.

Take this code, for example.

const myElement = document.getElementById("foo");

console.log(myElement.style.color); // a read
myElement.style.color = "blue"; // a write
console.log(myElement.style.color); // a read
myElement.style.color = "red"; // a write
console.log(myElement.style.color); // a read

This code will cause a few repaints in the browser, because all of the reads and writes happen in an interleaved manner. If we "schedule" our reads and writes into separate animation frames, we can basically achieve the same amount of rendering with much fewer repaints - which is done (in concept) like so:

console.log(myElement.style.color);

requestAnimationFrame(() => {
  myElement.style.color = "blue";
  myElement.style.color = "red";
  requestAnimationFrame(() => {
    console.log(myElement.style.color);
  });
});

Of course, this would become a pain to maintain, so we could write a scheduler that basically turns every DOM access task into an asynchronous one, by queing read and write tasks separately and flushing the queues periodically - this way the reads and writes are executed in ordered batches:

class DOMScheduler {
  constructor() {
    this.reads = [];
    this.writes = [];
    this.scheduled = false;
    this.raf = requestAnimationFrame.bind(window);
  }
  read(task) {
    this.reads.push(task);
    scheduleFlush(this);
  }
  write(task) {
    this.writes.push(task);
    scheduleFlush(this);
  }
  runTasks(tasks) {
    let task;
    while (task = tasks.shift()) {
      task();
    }
  }
}

function scheduleFlush(scheduler) {
  if (!scheduler.scheduled) {
    scheduler.scheduled = true;
    scheduler.raf(flush.bind(null, scheduler));
  }
}

function flush(scheduler) {
  const { reads, writes } = scheduler;
  
  scheduler.runTasks(reads);
  scheduler.runTasks(writes);
  
  scheduler.scheduled = false;
  
  if (reads.length || writes.length) {
    scheduleFlush(scheduler);
  }
}

const el = document.getElementById("example");
const fast = new DOMScheduler();

fast.read(() => {
  console.log(`color: "${el.style.color}"`) // ""
});

fast.write(() => {
  el.style.color = "blue";
});

fast.read(() => {
  console.log(`color: "${el.style.color}"`); // ""
});

fast.write(() => {
  el.style.color = "green";
  
  fast.read(() => {
    console.log(`color: "${el.style.color}"`) // "green"
  });
});

Just note that the above calls to fast.read and fast.write are asynchronous - regular DOM reads and writes are synchronous, so these will not behave as you might expect. If you want to make it a little easier, you can extend it into a promisified version:

class AsyncDOMScheduler extends DOMScheduler {
  read(task) {
    return new Promise(resolve => {
      super.read(() => resolve(task()));
    });
  }
  write(task) {
    return new Promise(resolve => {
      super.write(() => resolve(task()));
    });
  }
}

const el = document.getElementById("fps");
const fast = new AsyncDOMScheduler();

async function main() {

  await fast.read(() => {
    console.log(el.style.color); // ""
  });
  
  await fast.write(() => {
    el.style.color = "blue";
  });
  
  await fast.read(() => {
    console.log(el.style.color); // "blue"
  });
  
  await fast.write(() => {
    el.style.color = "green";
  });
  
  await fast.read(() => {
    console.log(el.style.color); // "green"
  });
  
}

main();

For more information on how and why this works, and a more robust and complete implementation, check out the FastDom library: https://github.com/wilsonpage/fastdom - note that you might not need this particular optimization if you're using a rendering framework, which should already be doing these sorts of optimisations for you.

Use Promises and concurrent scheduling

Try to look for areas in your code where you make use of asynchronous methods, especially if those areas deal with fetching remote resources in a serial manner.

For example, here is some code to load different sound effects into an AudioContext:

const sounds = {};
const soundUrls = [/*... urls to sound assets ...*/];

async function loadSounds(urls) {
  const buffers = [];
  for (let i = 0; i < urls.length; i++) {
    const buffer = await loadSound(url, i);
    buffers.push(buffer);
  }
  return buffers;
}

async function loadSound(url, id) {
  const response = await fetch(url);
  const data = await response.arrayBuffer();

  return new Promise((resolve) => {
    audioContext.decodeAudioData(data, (buffer) => {
      if (id) {
        sounds[id] = buffer;
      }
      resolve(buffer);
    });
  });
}

There is a problem with this code. It will load each sound asset one by one, one after the other. None of the sounds depend on one another in order to load, and we can take advantage of this to gain a substantial performance boost.

Instead of loading one sound at a time, we can load them simultaneously using Promise.all.

const sounds = {};
const soundUrls = [/*... urls to sound assets ...*/];

async function loadSounds(urls) {
  return Promise.all(soundUrls.map(loadSound));
}

async function loadSound(url, id) {
  const response = await fetch(url);
  const data = await response.arrayBuffer();

  return new Promise((resolve) => {
    audioContext.decodeAudioData(data, (buffer) => {
      if (id) {
        sounds[id] = buffer;
      }
      resolve(buffer);
    });
  });
}

Do not create too many event listeners

This is another one of those "don't bother if you're using a framework" tips, as a good framework should already be doing this for you. However, if you're not using a framework, then adding a bunch of event listeners to your game can cause quite a lot of cascading performance bottlenecks.

Take this code for example, which adds three event listeners - one for a click on three different buttons.

const button1 = document.getElementById("button1");
const button2 = document.getElementById("button1");
const button2 = document.getElementById("button1");

button1.addEventListener("click", () => {
  // do something with button1
});

button2.addEventListener("click", () => {
  // do something with button2
});

button3.addEventListener("click", () => {
  // do something with button3
});

At first glance, this code looks fine. Nothing's really wrong with it, aside from the fact that there are three separate functions taking up memory, which isn't a big deal.

What if we introduce more buttons, though? Well, we will want to make maintenance easier on us, so...

const buttons = document.querySelectorAll(".button");

for (const button of buttons) {
  button.addEventListener(() => {
    const buttonId = button.dataset.buttonId;
    if (buttonId === "1") {
      // do something with button 1...
    } else if (buttonId === "2") {
      // ...
    } // ...
  });
}

...and this code works, nicely. Except now 3 months have passed and there are now 400 buttons in our game, each with their own event listener, and this starts becoming an issue. First off, now we have to move the function out of the loop, so we aren't creating new instances of it on each iteration:

const buttons = document.querySelectorAll(".button");

for (const button of buttons) {
  button.addEventListener(handleButtonClick);
}

function handleButtonClick(event) {
  const button = event.target;
  const buttonId = button.dataset.buttonId;
  if (buttonId === "1") {
    // do something with button 1...
  } else if (buttonId === "2") {
    // ...
  } // ...
}

This is an improvement, but we still have 400 event listeners. We could do things like check if the buttons are actually actively displayed in our game, and conditionally apply the event listeners, that's one possible optimisation, but I won't get into that. It's just more branching complexity.

A couple of weeks pass, and we add a feature that lets a user add their own buttons. And we somehow forget to update the button code and for some reason any button the user adds doesn't register any clicks - this is because we're only iterating over the buttons that already existed when we registered the event listeners. So, we have to make sure that new buttons get their event listeners somehow... I won't get into this either.

As you can see, this is spiralling out of control. How do we fix it? The answer is to just register one event listener for a click on the entire document, and check the event target to see if it's something we should be interested in.

document.addEventListener("click", (event) => {
  if (event.target.matches(".button")) {
    // one of our buttons was clicked
    const button = event.target;
  }
});

How many event listeners is this? Just one. Not two, not 400, but one.

Will this event listener respond to elements created as a result of user input? Yes, yes it will.

It even lets us listen for clicks on things other than buttons!

Take advantage of CSS animations and their ability to utilize hardware acceleration

Animating with JS is nice, you can get a lot of precise control that you can't get with CSS. But for simple animations like fades, color changes, etc. it's better to use CSS.

Why?

Because CSS animations can take advantage of GPU acceleration. When you use properties like translateZ, translate3d, max-width, min-width, etc in your animations, CSS will shift the painting of those animations from the CPU over to the GPU. This is something that is not really possible in JS (unless you use something like GPU.js, but a whole dependency just for some silly animations? kek)

Plus, not only do you get GPU acceleration, but you can simplify your code as well. Instead of slowly incrementing/decrementing an opacity value over time via JS, you can just toggle a class on activation/deactivation. Simple. Easy. You can even bind it to your state.

Although a bit outdated, this article gives a good outline of this concept: https://www.smashingmagazine.com/2016/12/gpu-animation-doing-it-right/

Utilize the Cache API & Service Workers to prevent recurrent network roundtrips

Most games are full of static resources like images, sprite sheets, audio files, fonts, etc. These are all assets that need to be sent to the browser over the network.

If the player lacks an internet connection, some of these assets may be broken.

One can cache these assets in the browser so that they load even while the user is offline, and this has the added benefit of decreasing loading times for these assets.

This used to be done via the HTML5 ApplicationCache, which has been deprecated in favor of the Cache API, which requires Service Workers. Thankfully, the Cache API is currently supported in all modern browsers.

Here is a tutorial on how to get started with the Cache API: https://web.dev/cache-api-quick-guide/

Utilize Web Workers to offload heavy computations to another thread

Web Workers can do quite a lot for gains in performance. Think of web workers as threads, but in the browser.

Any JS script can spawn a new web worker that it has exclusive access to, or a shared worker that other scripts can share access to.

Web workers are an isolated execution context that executes code in a limited environment - they have access to a number of APIs, but do not have access to the DOM.

They are also very easy to use. You simply pass messages around and react to messages to communicate between your workers and your main script, like so:

In main.js:

const counterWorker = new Worker("counterWorker.js");
const counter = document.getElementById("counter");
const increment = document.getElementById("increment");
const decrement = document.getElementById("decrement");

counterWorker.onmessage = (message) => {
  counter.textContent = message.data;
};

increment.addEventListener("click", () => {
  counterWorker.postMessage("increment");
});

decrement.addEventListener("click", () => {
  counterWorker.postMessage("decrement");
});

In counterWorker.js:

let count = 0;

postMessage(count);

onmessage = (message) => {
  if (message.data === "increment") {
    count += 1;
  }

  if (message.data === "decrement") {
    count -= 1;
  }

  postMessage(count);
}

Note that in the worker, postMessage and onmessage aren't attached to anything, since the worker itself is basically the global scope.

Of course, using a whole thread for something as simple as an incrementing counter is a little bit overkill, but it makes a big difference when you have expensive math calculations to perform and you can perform them on a separate thread.

Offloading computationally expensive code like this to a separate thread allows you to keep your main thread for just rendering purposes, keeping your user interface snappy and performant.

See more information here: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

Don't act on entities the player cannot see

If you have objects that are out-of-view, (whether a spaceship in a game off to the side of the screen or a resource counter hidden behind a tab in a component), do not bother updating their views. They might exist in the document at the time that the player is playing a game, but just because they exist in the document, that doesn't mean the player can actually see them.

Performing calculations to update all of these hidden things could cost quite a bit of resources and cause a number of unnecessary browser repaints.

Instead, simply ignore them. Be sure to keep your game's state updated, and only update these elements or objects with their new state when they come back into view.

You can use the IntersectionObserver API to detect if document elements are currently in view. For canvas objects, just check if their x and y coordinates exceed the canvas width / height or are less than 0.

Using the IntersectionObserver API is nice, but if you can get away with not using it, even better. It is less necessary if you derive your view from your state and only render elements/objects if the state requires that they be rendered.

If your game has complex objects that have their own "intelligence" and produce events on their own, you still don't need to update their views if they exist off screen. Simply attach these objects to a State Machine and update the state machine instead. If their state requires that they be rendered (for example a sheep has walked within view of the player), then by all means, render it.

Reduce the number of logical branching pathes via polymorphism

In programming, we make use of all sorts of syntax constructs, like conditional statements, ternary operators, functions, loops, binary / logical operators, etc.

Some of these constructs result in what we call "logical branches". A logical branch is essentially a point during a program's execution where the program must inspect some state and then choose between two or more "paths" of execution. These are found typically in conditional statements:

// two potential "paths" / "branches"
if (a) {

} else {

}

Or in logical comparisons:

const c = a || b; // two potential paths

// two potential paths
if (c) {

} else {

}

// ... totalling 4 paths

When the interpreter (or compiler) comes to these sections of code, it performs a little calculation to determine where in the code to go next. Though almost entirely negligible, this little calculation does take a little bit of time, and in larger more complex programs, with many potential users, this time can add up.

There is a concept known as a "polymorphism" in functional programming, where we can take advantage of the type to remove some of this branching complexity.

Take a Result union type, for example. It can be one of two possible unions:

class Result {
  constructor(value) {
    if (!arguments.length) {
      throw new Error("Result needs a value");
    }
    this.value = value;
  }
}

class Success extends Result {}

class Failure extends Result {}

We have both a Success and a Failure type, that both belong to the same union - Result. If we get a Result and want to act upon it (for example, transform it's value), we first have to inspect which of the two types we're working with. Let's do that with a simple map utility:

function map(result, transform) {
  if (result instanceof Failure) {
    return result;
  } else if (result instanceof Success) {
    return new Success(transform(value));
  }
}

This utility could be used in many places in our projects, and each time we use it, we are creating a branching path.

There's an optimization we can make here, to remove this branching, via a polymorphism of the map utility on each type. All we have to do is adjust the implementations of our types to implement this interface:

class Success extends Result {
  map(transform) {
    return new Success(transform(this.value));
  }
}

class Failure extends Result {
  map(transform) {
    return this;
  }
}

Now, we just adjust our utility to call this interface:

function map(result, transform) {
  return result.map(transform);
}

Thus, we can use our utility, and it won't ever create any logical branches. It just defers to the interface of the type.

Of course, this can be achieved without using union types. A realistic example would be an event handler that responds to different actions, or anywhere a switch case might be used. let's say we have an event handler that receives a message object, and we do different things according to the message's type property (much like we would do in Redux or in our button event handler we saw earlier).

Normally we would inspect this property via a switch/case statement, which could have many potential logical branches (based on how many different types we have), but instead of doing that, we could simply use a dictionary:

const messageHandlers = {
  increment() {
    // do something here
  },
  decrement() {
    // do something here
  }
}

function onMessage(message) {
  messageHandlers[message.type](message);
}

As you can see, there are no logical branches in this code, yet we are still able to perform different functions based on different inputs. This scales to however many different types our message handler deals with.

Of course, care needs to be taken here. There are certainly cases where deferring logic to a function via polymorphism could in fact be more computationally expensive than just checking a condition, depending on what the function is doing and how computationally expensive it is to call. Be sure to routinely profile your code to be sure you're seeing improvements. Don't just optimise for the sake of it.

Don't perform unnecessary calculations

As your idea develops more and fleshes itself out, you might add a lot of code for lots of different parts of your game. You might start seeing areas where code performs calculations that are not strictly necessary.

Since we're in a game sub, I will give a perfect game-related example: collision detection.

Let's assume we have a bunch of objects on our game's canvas, and we want to check if two objects are colliding:

for (let i = 0; i < gameObjects.length; i++) {
  const object1 = gameObjects[i];
  object1.isColliding = false;

  for (let j = 0; j < gameObjects.length; j++) {
    const object2 = gameObjects[j];

    // skip the case where we check if an object is colliding with itself
    if (object1 === object2) {
      continue; 
    }

    if (checkCollision(object1, object2)) {
      object1.isColliding = true;
      object2.isColliding = true;
    }
  }
}

This code is straightforward, and it works. We loop through each game object, reset it's collision status to false, then loop through each game object again to check if our current object from the outer loop is colliding with an object in the inner loop. We be sure to ignore the case where both the inner and outer objects reference the same object, because we know that an object will always be colliding with itself.

However, there is a major issue here. If we've checked if an object is colliding, we don't need to check it again, because we know it's colliding already, so we can actually start our collision check with only the remaining objects:

for (let i = 0; i < gameObjects.length; i++) {
  const object1 = gameObjects[i];
  object1.isColliding = false;
  
  for (let j = i; j < gameObjects.length; j++) {
    const object2 = gameObjects[j];

    if (object1 === object2) continue;

    if (checkCollision(object1, object2)) {
      object1.isColliding = true;
      object2.isColliding = true;
    }
  }
}

This is already much better, we're not performing any unnecessary calculations anymore, but there's still one more optimisation we can make. We know that the i index references the current object, which means that the first object that is inspected in our inner loop corresponds to the object in our outer loop - so we can simply shift the j index by 1 to ignore it, and remove our conditional branch that deals with ignoring the case where both objects reference the same thing:

for (let i = 0; i < gameObjects.length; i++) {
  const object1 = gameObjects[i];
  object1.isColliding = false;
  
  for (let j = i + 1; j < gameObjects.length; j++) {
    const object2 = gameObjects[j];

    if (checkCollision(object1, object2)) {
      object1.isColliding = true;
      object2.isColliding = true;
    }
  }
}

Now, our code for doing collision detection doesn't perform any unnecessary checks. Since collision detection is quite a computationally heavy task, especially if there are hundreds our thousands of objects in our game, this little optimisation could have saved us potentially a lot of execution time.

There's actually more optimisations we can make here - we don't exactly need to check every object in our game for collisions, like this code is doing.

For a simple 2D game, we could divide the "game world" into a grid of tiles, and only check the objects that exist in the same tile as the object that we're currently inspecting.

For a 3D game, we could use an algorithm that puts the entire game world into a giant invisible cube, then divides that cube into 8 smaller invisible cubes, and each of those cubes into 8 smaller invisible cubes, etc. Then we can take an object, find out which "cube" it is in by checking it's coordinates against the coordinates of the lowest level of cubes - if it exists inside two cubes simultaneously, then we move "up" a level to get a bigger cube - and only check for collisions on objects that exist inside the same cube.

We could also adjust our collision detection function to check a boxed area around the object we're checking (known as a "hitbox"), so we don't match on each and every pixel.

This is an involved example, but this sort of problem arises almost anywhere in a program, not just in things like collision detection, so you should definitely keep an eye out for optimisations you can make that reduce unnecessary computations, as these optimisations will generally net you the most performance gains.

Conclusion

Hopefully all of these tips are useful to some of you, as this post took me a lot longer to write than I thought it would 😅.

Posted Apr 23, 2021, 2:55 PM UTC

17121
Loading comments…