Changing Canvas State (v4)

Revision 4 of this benchmark created on


Description

Compares rendering an alternating pinstripe pattern with two approaches:

  1. draw serially, alternating color each time
  2. draw with two passes, changing color once only

Preparation HTML

<canvas id="c" width="640" height="480"></canvas>
<script>
  var canvas = document.getElementById('c');
  var context = canvas.getContext('2d');
  
  var GAP = 10;
  var COLOR1 = 'red';
  var COLOR2 = 'blue';
  var STRIPES = 20;
</script>

Setup

context.clearRect(0, 0, canvas.width, canvas.height);

Test runner

Ready to run.

Testing in
TestOps/sec
serially
for (var i = 0; i < STRIPES; i++) {
  context.fillStyle = (i % 2 ? COLOR1 : COLOR2);
  context.fillRect(i * GAP, 0, GAP, 480);
}
ready
two passes
context.fillStyle = COLOR2;
for (var i = 0; i < STRIPES/2; i++) {
  context.fillRect((i*2) * GAP, 0, GAP, 480);
}
context.fillStyle = COLOR1;
for (var i = 0; i < STRIPES/2; i++) {
  context.fillRect((i*2+1) * GAP, 0, GAP, 480);
}
ready
four passes (height)
context.fillStyle = COLOR2;
for (var i = 0; i < STRIPES/2; i++) {
  context.fillRect((i*2) * GAP, 0, GAP, 240);
}
for (var i = 0; i < STRIPES/2; i++) {
  context.fillRect((i*2) * GAP, 241, GAP, 260);
}
context.fillStyle = COLOR1;
for (var i = 0; i < STRIPES/2; i++) {
  context.fillRect((i*2+1) * GAP, 0, GAP, 240);
}
context.fillStyle = COLOR1;
for (var i = 0; i < STRIPES/2; i++) {
  context.fillRect((i*2+1) * GAP, 241, GAP, 260);
}
ready

Revisions

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