Partial Re-rendering

JavaScript performance comparison

Revision 2 of this test case created by Boris


How much faster is it to render just the parts of the canvas that changed, compared to the whole thing?

Rendering a white dot that moves across a black background.

Preparation code

<canvas id="c" width="640" height="480"></canvas>
  var canvas = document.getElementById('c');
  var context = canvas.getContext('2d');
  var HEIGHT = 100;

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
re-render whole scene
for (var i = 0; i < canvas.width; i++) {
  context.fillStyle = '#000';
  context.fillRect(0, 0, canvas.width, canvas.height);
  context.fillStyle = '#fff';
  context.fillRect(i, HEIGHT, 1, 1);
only render differences
for (var i = 0; i < canvas.width; i++) {
  context.fillStyle = 'black';
  context.fillRect(i-1, HEIGHT, 1, 1);
  context.fillStyle = 'white';
  context.fillRect(i, HEIGHT, 1, 1);

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Brennan commented :

Is there a way to get a real life example with this in use?

Bonsaiheld commented :

Hm... Very nice. The differences are HUGE. I need this badly for my game.

Question: So this works with pixels... How to do it with sprite images? I think i did it wrong. When i make each player in my game itself clearing its surrounding rectangle, then - when two players overlap each other - the one player cuts off the other inside its rectangle. :/

Comment form temporarily disabled.

Add a comment