canvas-blitting-sparse

JavaScript performance comparison

Test case created by Ilmari

Preparation code

<script>
  var c = document.createElement('canvas');
  c.width = c.height = 256;
  var ctx = c.getContext('2d');
  
  var framebuffer = ctx.getImageData(0, 0, 256, 256);
</script>
    

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
fillRect
ctx.clearRect(0, 0, 256, 256);
for (var j = 0; j < 16; j++) {
 var xoff = Math.cos(j / 16 * Math.PI * 2) * 32 + 128;
 var yoff = Math.sin(j / 16 * Math.PI * 2) * 32 + 128;
 for (var i = 0; i < 256; i++) {
  var x = Math.cos(i / 256 * Math.PI * 2) * 64 + xoff;
  var y = Math.sin(i / 256 * Math.PI * 2) * 64 + yoff;
  ctx.fillRect(x, y, 1, 1);
 }
}
pending…
putImageData
var px = framebuffer.data;
for (var i = 0; i < px.length; i++) {
 px[i] = 0;
}
for (var j = 0; j < 16; j++) {
 var xoff = Math.cos(j / 16 * Math.PI * 2) * 32 + 128;
 var yoff = Math.sin(j / 16 * Math.PI * 2) * 32 + 128;
 for (var i = 0; i < 256; i++) {
  var x = Math.cos(i / 256 * Math.PI * 2) * 64 + xoff;
  var y = Math.sin(i / 256 * Math.PI * 2) * 64 + yoff;
  var off = (y * 256 + x) * 4;
  px[off] = 0;
  px[off + 1] = 0;
  px[off + 2] = 0;
  px[off + 3] = 255;
 }
}
ctx.putImageData(framebuffer, 0, 0);
pending…
putImageData with fresh buffer
ctx.clearRect(0, 0, 256, 256);
var framebufferi = ctx.getImageData(0, 0, 256, 256);
var px = framebufferi.data;
for (var j = 0; j < 16; j++) {
 var xoff = Math.cos(j / 16 * Math.PI * 2) * 32 + 128;
 var yoff = Math.sin(j / 16 * Math.PI * 2) * 32 + 128;
 for (var i = 0; i < 256; i++) {
  var x = Math.cos(i / 256 * Math.PI * 2) * 64 + xoff;
  var y = Math.sin(i / 256 * Math.PI * 2) * 64 + yoff;
  var off = (y * 256 + x) * 4;
  px[off] = 0;
  px[off + 1] = 0;
  px[off + 2] = 0;
  px[off + 3] = 255;
 }
}
ctx.putImageData(framebufferi, 0, 0);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments