drawImage vs CanvasPattern (v13)

Revision 13 of this benchmark created on


Description

For drawing sprites, how does fillRect() with a CanvasPattern compare to drawImage?

Preparation HTML

<canvas id="screen" width="48" height="48">
</canvas>
<canvas id="big-screen" width="432" height="432">
</canvas>
<canvas id="imgCached" width="48" height="48" style="display:none;">
</canvas>
<script>
  var ctx = document.getElementById("screen").getContext("2d");
  var bigCtx = document.getElementById("big-screen").getContext("2d");
  var imgCached = document.getElementById("imgCached");
  var patternRepeat, patternNoRepeat;
  var image = document.createElement("img");
  var width, height;
  image.onload = function() {
    patternNoRepeat = ctx.createPattern(image, "no-repeat");
    patternRepeat = ctx.createPattern(image, "repeat");
    imgCached.getContext("2d").drawImage(image, 0, 0);
    width = image.width;
    height = image.height;
  };
  image.src = "http://icons.iconarchive.com/icons/ph03nyx/super-mario/48/Retro-Mushroom-1UP-2-icon.png";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Image
ctx.drawImage(image, 0, 0);
ready
No repeat
ctx.fillStyle = patternNoRepeat;
ctx.fillRect(0, 0, width, height);
ready
Repeat
ctx.fillStyle = patternRepeat;
ctx.fillRect(0, 0, width, height);
ready
Canvas
ctx.drawImage(imgCached, 0, 0);
ready
image on big screen
for (var i = 0; i < 9; ++i) {
  var tmpw = i * width;
  for (var j = 0; j < 9; ++j) {
    bigCtx.drawImage(image, tmpw, j * height);
  }
}
ready
pattern on big screen
bigCtx.fillStyle = patternRepeat;
bigCtx.fillRect(0, 0, width * 9, height * 9);
ready
Canvas on big screen
for (var i = 0; i < 9; ++i) {
  var tmpw = i * width;
  for (var j = 0; j < 9; ++j) {
    bigCtx.drawImage(imgCached, tmpw, j * height);
  }
}
ready

Revisions

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