Canvas Float Point vs Rounded Points

JavaScript performance comparison

Test case created by Jack Rugile

Preparation code

<canvas id="c" width="500" height="500"></canvas>
<script>
Benchmark.prototype.setup = function() {
    var c = document.getElementById('c');
    var ctx = c.getContext('2d');
    var cw = c.width;
    var ch = c.height;
    var rand = function(rMi, rMa){return ~~((Math.random()*(rMa-rMi+1))+rMi);}
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
float
var x1 = Math.PI;
var y1 = Math.PI*2;
var x2 = Math.PI*3;
var y2 = Math.PI*4;
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.stroke();
pending…
round
var x1 = Math.PI;
var y1 = Math.PI*2;
var x2 = Math.PI*3;
var y2 = Math.PI*4;
ctx.beginPath();
ctx.moveTo(Math.round(x1),Math.round(y1));
ctx.lineTo(Math.round(x2),Math.round(y2));
ctx.stroke();
pending…
newround
var x1 = Math.PI;
var y1 = Math.PI*2;
var x2 = Math.PI*3;
var y2 = Math.PI*4;
ctx.beginPath();
ctx.moveTo(~~x1,~~y1);
ctx.lineTo(~~x2,~~y2);
ctx.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. Here’s a list of current revisions for this page:

0 comments

Add a comment