Setting one HTML5 Canvas Pixel

JavaScript performance comparison

Revision 46 of this test case created by Henri

Preparation code

<p>(Scroll down to see the 'Run Tests' button.)</p>
<canvas id="c" width="800" height="300"></canvas>
<script>
  $c = document.getElementById('c');
  $ctx = $c.getContext('2d');
  $ctx.clearRect(0, 0, 800, 300);
  $px = $ctx.createImageData(1, 1);
var cpx=$ctx.getImageData(0,0,800, 300)
  $pxls = [];
  // Precompute random pixels so this time isn't included in the tests
  for (var i = 0; i < 10000; ++i) $pxls.push({
   x: Math.random() * 800 << 0,
   y: Math.random() * 300 << 0,
   r: Math.random() * 255 << 0,
   g: Math.random() * 255 << 0,
   b: Math.random() * 255 << 0,
   a: Math.random() * 128 << 0 + 128
  });
  $i = 0;
</script>
    

Preparation code output

<p>(Scroll down to see the 'Run Tests' button.)</p> <canvas id="c" width="800" height="300"></canvas>

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
get/putImageData
var px = $pxls[$i++ % 10000];
var idata = $ctx.getImageData(0, 0, 800, 300);
var d = idata.data;
var o = (px.y * 800 + px.x) * 4;
d[o] = 0;
d[o + 1] = 0;
d[o + 2] = 0;
d[o + 3] = px.a;
$ctx.putImageData(idata, 0, 0, 20, 20, 20, 20);
pending…
fillRect
var px = $pxls[$i++ % 10000];
$ctx.fillStyle = 'rgba(0,0,0,1)';
$ctx.fillRect(px.x, px.y, 20, 20);
pending…
fillRect with hex color & globalalpha
var px = $pxls[$i++ % 10000];
$ctx.fillStyle = '#000000';
$ctx.globalAlpha = 1;
$ctx.fillRect(px.x, px.y, 20, 20);
pending…
using original image data with dirty
var px = $pxls[$i++ % 10000];
index = (px.x + px.y * cpx.width) * 4;
cpx.data[index] = 0;
cpx.data[index + 1] = 0;
cpx.data[index + 2] = 0;
cpx.data[index + 3] = 1;
$ctx.putImageData(cpx, 0, 0, px.x, px.y, 20, 20);
pending…
fillRect with globalAlpha
var px = $pxls[$i++ % 10000];
$ctx.fillStyle = 'rgb(0,0,0)';
$ctx.globalAlpha = (px.a / 255);
$ctx.fillRect(px.x, px.y, 20, 20);
pending…

Compare results of other browsers

Revisions

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

0 Comments

(Scroll down to see the 'Run Tests' button.)