Trig Lookup

JavaScript performance comparison

Test case created by ConstableBrew

Info

Comparing the speed of a pre-calculated lookup table against the Math.cos and Math.sin functions.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var fastSin = (function() {
      var sinTable = [],
        DEGtoRAD = Math.PI / 180,
        RADtoDEG = 180 / Math.PI;
      for (var i = 0; i < 360; ++i) {
        sinTable.push(Math.sin(i * DEGtoRAD));
      }
      var func = function(a) {
        return sinTable[~~(a * RADtoDEG)];
      };
      return func;
    }());
   
    var retval, a = Math.random() * Math.PI * 2;
};
</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
Math.sin
retval = Math.sin(a);
pending…
sin Lookup
retval = fastSin(a);
pending…

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

Compare results of other browsers

0 comments

Add a comment