Batching Line Drawing Calls (v5)

Revision 5 of this benchmark created by William R. J. Ribeiro on


Description

Compares drawing a simple shape out of multiple lines compared to one polyline.

Preparation HTML

<canvas id="c" width="640" height="480">
</canvas>
<script>
  var canvas = document.getElementById('c');
  var context = canvas.getContext('2d');
  var points = [];
  var generateRandomPoints = function(points, amount) {
      for (var i = amount - 1; i > 0; i--) {
        var x = Math.floor(Math.random() * 100);
        var y = Math.floor(Math.random() * 100);
        points[i] = {
          x: x,
          y: y
        };
      }
      };
  generateRandomPoints(points, 60);
  context.strokeStyle = 'black';
</script>

Setup

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

Test runner

Ready to run.

Testing in
TestOps/sec
multiple lines
for (var i = points.length - 1; i > 1; i--) {
  var p = points[i];
  var p1 = points[i - 1];
  context.beginPath();
  context.moveTo(p.x, p.y);
  context.lineTo(p1.x, p1.y);
  context.stroke();
}
ready
one polyline
context.beginPath();
for (var i = points.length - 1; i > 1; i--) {
  var p = points[i];
  var p1 = points[i - 1];
  context.moveTo(p.x, p.y);
  context.lineTo(p1.x, p1.y);
}
context.stroke();
ready

Revisions

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