Canvas Pixel Manipulation (v140)

Revision 140 of this benchmark created by cast to uint8 on


Description

Tests two different methods of manipulating pixels using the canvas.

NOTE: Now takes endianness into account.

Preparation HTML

<canvas id="canvas" height="256" width="256"></canvas>

Setup

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 length = data.length;
    
    var value = -1;
    
    var buf = new ArrayBuffer(imageData.data.length);
    var buf8 = new Uint8ClampedArray(buf);
    var data32 = new Uint32Array(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;
    }
    
    var max = Math.max, min = Math.min;

Test runner

Ready to run.

Testing in
TestOps/sec
with min/max
for(var i = 0;i<length;i+=4) {
   data[i]   = max(0, data[i] + value);    // red
   data[i+1] = max(0, data[i+1] + value);    // green
   data[i+2] = max(0, data[i+2] + value);    // blue
   data[i+3] = 255;      // alpha
}
ctx.putImageData(imageData, 0, 0);
ready
without min/max
for(var i = 0;i<length;i+=4) {
   data[i]   = data[i] + value;    // red
   data[i+1] = data[i+1] + value;    // green
   data[i+2] = data[i+2] + value;    // blue
   data[i+3] = 255;      // alpha
}
ctx.putImageData(imageData, 0, 0);
ready

Revisions

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