Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31
Chrome 117
Windows
Desktop
one year ago
Test name Executions per second
1*1 fillRect/concat 0.6 Ops/sec
Image Data 1.3 Ops/sec
HTML Preparation code:
AخA
 
1
<canvas id="c" width="1000" height="500"></canvas>
Script Preparation code:
 
$c = document.getElementById('c');
$ctx = $c.getContext('2d');
$ctx.clearRect(0, 0, 10000, 500);
$px = $ctx.createImageData(1000, 500);
$pxls = [];
for (var i=0; i<500000; ++i) $pxls.push({
   x: Math.random() * 1000 << 0,
   y: Math.random() * 500 << 0,
   r: Math.random() * 255 << 0,
   g: Math.random() * 255 << 0,
   b: Math.random() * 255 << 0,
   a: Math.random() * 128 << 0 + 128
});
$i = 0;
Tests:
  • 1*1 fillRect/concat

     
    for (var i=1000;i--;){
      for(var j=500;j--;){
        var px = $pxls[i*j]
        $ctx.fillStyle = 'rgba(' + px.r + ',' + px.g + ',' + px.b + ',' + (px.a / 255) + ')';
        $ctx.fillRect(px.x, px.y, 1, 1);
      }
    }
  • Image Data

     
    for (var i=0; i<500000; ++i){
      var px=$pxls[i], d=$px.data[i];
    d[0] = px.r;
    d[1] = px.g;
    d[2] = px.b;
    d[3] = px.a;
    }
    $ctx.putImageData($px, 1000,500);