Changing Canvas State, optimization tries (v7)

Revision 7 of this benchmark created by bcd on


Description

Compares rendering an alternating pinstripe pattern.

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
always set fillStyle
for (var i = 0; i < STRIPES/2; i++) {
  context.fillStyle = COLOR2;
  context.fillRect((i*2) * GAP, 0, GAP, 480);
}

for (var i = 0; i < STRIPES/2; i++) {
  context.fillStyle = COLOR1;
  context.fillRect((i*2+1) * GAP, 0, GAP, 480);
}
ready
Reduce drawing calls
context.fillStyle = COLOR2
context.fillRect(0, 0, STRIPES * GAP, 480);

context.fillStyle = COLOR1;
for (var i = 0; i < STRIPES/2; i++) {
  context.fillRect((i*2+1) * GAP, 0, GAP, 480);
}
ready
Reduce calculations
context.fillStyle = COLOR2
context.fillRect(0, 0, STRIPES * GAP, 480);

context.fillStyle = COLOR1;
for (var i = 1; i < STRIPES; i+=2) {
  context.fillRect(i * GAP, 0, GAP, 480);
}
ready

Revisions

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