acos vs array

JavaScript performance comparison

Test case created by shhac

Preparation code

<script type="text/javascript">
var i, j, acosTable = new Array(3601), v1, v2, cos;
for (i = 0; i <= 3600; i += 1) {
    j = -1 + i / 1800;
    acosTable[i] = [j, Math.acos(j)];
}
</script>
<script>
Benchmark.prototype.setup = function() {
    v1 = {x: ~~(Math.random() * 11), y: ~~(Math.random() * 11)};
    v2 = {x: ~~(Math.random() * 11), y: ~~(Math.random() * 11)};
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
acos
// direct compute
Math.acos( (v1.x * v2.x + v1.y * v2.y) / ( Math.sqrt(v1.x*v1.x + v1.y*v1.y) * Math.sqrt(v2.x*v2.x + v2.y*v2.y) ) );
pending…
table
// cos
cos = (v1.x * v2.x + v1.y * v2.y) / ( Math.sqrt(v1.x*v1.x + v1.y*v1.y) * Math.sqrt(v2.x*v2.x + v2.y*v2.y) );

// convert to table value
i = -1;
while(++i <= 3600 && Math.abs(acosTable[i][0] - cos) >= 1/1800){}
acosTable[i][1];
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