Bresenham Comparison

JavaScript performance comparison

Revision 3 of this test case created by Steven Sell

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function drawHorizontal(x0, y0, dx, dy, dir, ydir) {
      var dx2 = dx * 2;
      var dy2 = dy * 2;
      var error = dy2 - dx;
      
      var start = dx;
      
      while(dx--) {        
          if(error >= 0) {
              y0 += ydir;
              error -= dx2;
          }
          
          error += dy2;
          x0 += dir;
      }
  }
  
  function drawVertical(x0, y0, dx, dy, dir, ydir) {
      var dx2 = dx * 2;
      var dy2 = dy * 2;
      var error = dx2 - dy;
      
      var start = dy;
      
      while(dy--) {
          if(error >= 0) {
              x0 += dir;
              error -= dy2;
          }
  
          error += dx2;
          y0 += ydir;
      }
  }
  
  var x = [ 5, 4, 3, 2, 1, -1, -2, -3, -4, -5 ];
  var y = [ 1, -2, 3, -4, 5, -5, 4, -3, 2, -1 ];
  
  var i = 0;

};
</script>

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
Bresenham
function drawBresenhamLine(x0, y0, x1, y1) {
    if(y0 > y1) {
        y0 = y0 + y1;
        y1 = y0 - y1;
        y0 = y0 - y1;
        
        x0 = x0 + x1;
        x1 = x0 - x1;
        x0 = x0 - x1;
    }
    
    var dx = x1 - x0;
    var dy = y1 - y0;
    
    if(dx > 0) {
        if(dx > dy) {
            drawHorizontal(x0, y0, dx, dy, 1, 1);
        } else {
            drawVertical(x0, y0, dx, dy, 1, 1);
        }
    } else {
        dx = -dx;
        if(dx > dy) {
            drawHorizontal(x0, y0, dx, dy, -1, 1)
        } else {
            drawVertical(x0, y0, dx, dy, -1, 1);
        }
    }
}

drawBresenhamLine(x[i % 10], y[i % 10], 10, 0);
i++;
pending…
Variable Length Bresenham
function drawBresenhamLine(x0, y0, x1, y1) {
    var dx = x1 - x0;
    var dy = y1 - y0;
    var absX = Math.abs(dx);
    var absY = Math.abs(dy);
    var dirX = (dx > 0) ? 1 : -1;
    var dirY = (dy > 0) ? 1 : -1;

    if(absX > absY)
    {
        drawHorizontal(x0, y0, absX, absY, dirX, dirY);
    }
    else
    {
        drawVertical(x0, y0, absX, absY, dirX, dirY);
    }
}

drawBresenhamLine(x[i % 10], y[i % 10], 10, 0);
i++;
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.

0 Comments