50 points single stroke vs multiple (v41)

Revision 41 of this benchmark created 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');
 // context.lineJoin = 'miter';
context.lineWidth = 2;
  var points = [];
  var generateRandomPoints = function(points, amount) {
      for (var i = 0; i < amount; i++) {
        var x = i * 10;
        var y = Math.floor(Math.random() * 100);
        points[i] = {
          x: x,
          y: y
        };
      }
      };
  generateRandomPoints(points, 40); 
  context.strokeStyle = 'black';
</script>

Setup

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

Test runner

Ready to run.

Testing in
TestOps/sec
multiple lines
context.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 1; i < points.length; i++) {
  var p = points[i];
  var p1 = points[i - 1];
  context.beginPath();
  context.moveTo(p1.x, p1.y);
  context.lineTo(p.x, p.y);
  context.stroke();
}
ready
one polyline
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.moveTo(points[0].x, points[0].y);
for (var i = 1; i < points.length; i++) {
  var p = points[i];
  context.lineTo(p.x, p.y);
}
context.stroke();
ready
one fill
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
for (var i = 1; i < points.length; i++) {
  var p = points[i];
  context.rect(i * 7 + 5, 20, 7, 50)
}
context.fill();
ready
many fills
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
for (var i = 1; i < points.length; i++) {
  var p = points[i];
  context.fillRect(i * 7 + 5, 20, 7, 50)
}
 
ready

Revisions

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