Canvas Pixel Manipulation

JavaScript performance comparison

Revision 102 of this test case created

Info

Tests two different methods of manipulating pixels using the canvas.

NOTE: Now takes endianness into account.

Test calling sub function in a loop.

Preparation code

<canvas id="canvas" height="256" width="256"></canvas>
<script>
Benchmark.prototype.setup = function() {
    var canvas = document.getElementById('canvas');
    var canvasWidth = canvas.width;
    var canvasHeight = canvas.height;
    var ctx = canvas.getContext('2d');
    var imageData = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
   
    var data = imageData.data;
   
    var buf = new ArrayBuffer(imageData.data.length);
    var buf8 = new Uint8ClampedArray(buf);
    var data32 = new Uint32Array(buf);
    var view = new DataView(buf);
   
    // Determine whether Uint32 is little- or big-endian.
    data32[1] = 0x0a0b0c0d;
   
    var isLittleEndian = true;
    if (buf[4] === 0x0a && buf[5] === 0x0b && buf[6] === 0x0c &&
      buf[7] === 0x0d) {
      isLittleEndian = false;
    }
   
   
   
    function getPixel(data, x, y) {
      var val = data[y * canvasWidth + x];
      return [(val) & 0xFF, (val >> 8) & 0xFF, (val >> 16) & 0xFF, (val >> 24) & 0xFF];
    }
   
    function setPixel(data, x, y, r, g, b, a) {
      if (isLittleEndian) {
        data[y * canvasWidth + x] =
          (a << 24) | // alpha
        (b << 16) | // blue
        (g << 8) | // green
        r; // red
      } else {
        data[y * canvasWidth + x] =
          (r << 24) | // red
        (g << 16) | // green
        (b << 8) | // blue
        a; // alpha
   
      }
    }
   
    for (var y = 0; y < canvasHeight; ++y) {
      for (var x = 0; x < canvasWidth; ++x) {
        var index = (y * canvasWidth + x) * 4;
   
        var value = x * y & 0xff;
   
        data[index] = value; // red
        data[++index] = value; // green
        data[++index] = value; // blue
        data[++index] = 255; // alpha
      }
    }
};

Benchmark.prototype.teardown = function() {
    ctx.putImageData(imageData, 0, 0);
};
</script>

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
8-bit Pixel Manipulation
for (var y = 0; y < canvasHeight; ++y) {
  for (var x = 0; x < canvasWidth; ++x) {
    var index = (y * canvasWidth + x) * 4;

    data[index] = data[index] / 2; // red
    data[++index] = data[index] / 2; // green
    data[++index] = data[index] / 2; // blue
    data[++index] = 255; // alpha
  }
}
pending…
32-bit Pixel Manipulation
  for (var y = 0; y < canvasHeight; ++y) {
    for (var x = 0; x < canvasWidth; ++x) {
      var val = data32[y * canvasWidth + x];
      var r = (val) & 0xFF;
      var g = (val >> 8) & 0xFF;
      var b = (val >> 16) & 0xFF;
      var a = (val >> 24) & 0xFF;

      data32[y * canvasWidth + x] =
        (255 << 24) | // alpha
      ((value / 2) << 16) | // blue
      ((value / 2) << 8) | // green
      (value / 2); // red
    }
  }

  imageData.data.set(buf8);
pending…
32-bit Pixel Manipulation with get & setPixel call
  for (var y = 0; y < canvasHeight; ++y) {
    for (var x = 0; x < canvasWidth; ++x) {
      var index = (y * canvasWidth + x) * 4;

      setPixel(data, x, y, data[index] / 2, data[index + 1] / 2, data[index + 2] / 3, 255);
    }
  }

  imageData.data.set(buf8);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment