canvas loops

Benchmark created on


Preparation HTML

<script>
  // Create a 35x35 Canvas block.
  var canvasNode = document.createElement('canvas');
  canvasNode.width = "35";
  canvasNode.height = "35"
  
  // We must put this node into the body, otherwise
  // Safari Windows does not report correctly.
  canvasNode.style.display = 'none';
  document.body.appendChild(canvasNode);
  var ctx = canvasNode.getContext('2d');
  
  // draw a black letter 'O', 32px Arial.
  ctx.textBaseline = "top";
  ctx.font = "32px Arial";
  ctx.fillStyle = "black";
  ctx.strokeStyle = "black";
  
  ctx.fillText("O", 0, 0);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
stored data
var imageData = ctx.getImageData(0, 0, canvasNode.width, canvasNode.height)

for (var x = 8; x <= 32; x++) {
 for (var y = 1; y <= 32; y++) {

  // Indexes of the pixels in the array
  //idx uses the x, y, and the width to find the right place in the data array. uses *4 because each pixel has 4 entries for the 4 channels
  var idx = (x + y * imageData.width) * 4;
  var alpha = imageData.data[idx + 3]
  console.log(alpha)

  if (alpha != 255 && alpha != 0) {
   console.timeEnd('speed')
   return true; // font-smoothing must be on.
  }
 }
}
ready
data at point
for (var j = 8; j <= 32; j++) {
 for (var i = 1; i <= 32; i++) {

  var imageData = ctx.getImageData(i, j, 1, 1).data;
  var alpha = imageData[3];
  console.log(alpha)

  if (alpha != 255 && alpha != 0) {
   console.timeEnd('speed')
   return true; // font-smoothing must be on.
  }
 }

}
ready

Revisions

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