Batching Line Drawing Calls

JavaScript performance comparison

Revision 2 of this test case created by Boris

Preparation code

<canvas id="c" width="640" height="480"></canvas>
<script>
  var canvas = document.getElementById('c');
  var context = canvas.getContext('2d');
  context.strokeStyle = 'black';
</script>
      
<script>
Benchmark.prototype.setup = function() {
  context.clearRect(0, 0, canvas.width, canvas.height);

};
</script>

Preparation code output

<canvas id="c" width="640" height="480"></canvas>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
multiple lines
context.beginPath();
context.moveTo(100, 100);
context.lineTo(540, 380);
context.stroke();

context.beginPath();
context.moveTo(100, 380);
context.lineTo(540, 100);
context.stroke();
pending…
one polyline
context.beginPath();
context.moveTo(100, 100);
context.lineTo(540, 380);
context.moveTo(100, 380);
context.lineTo(540, 100);
context.stroke();
pending…

Compare results of other browsers

Revisions

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

1 Comment

Paul Irish commented :

rev 1: when its together the larger square area means when it blits it has to composite a larger draw call.

the bounding box of that call is enormous vs the bounding box of the indiv ones.

rev 2: when each draw call (big or small) affects the same rectangle of canvas, the results are more what you'd expect

(good call, @kurrik)