Trig functions vs lookup table (v2)

Revision 2 of this benchmark created on


Description

Comparing the speed of Math trig functions vs pre-computing the values into a lookup table.

Turns out trig is surprisingly fast in Firefox!

Setup

var sinTable = new Float32Array(21600);
    var a;
    for (var rep = 0; rep < 21600; rep++) {
      sinTable[rep] = (Math.sin((2 * Math.PI) * (rep / 21600)));
    }

Test runner

Ready to run.

Testing in
TestOps/sec
1) lookup table
for (var rep = 0; rep < 21600; rep++) {
  a = sinTable[rep];
}
ready
2) Math.sin
for (var rep = 0; rep < 21600; rep++) {
  a = Math.sin((2 * Math.PI) * (rep / 21599));
}
ready
3) Math.cos
for (var rep = 0; rep < 21600; rep++) {
  a = Math.cos((2 * Math.PI) * (rep / 21599));
}
ready
4) Math.tan
for (var rep = 0; rep < 21600; rep++) {
  a = Math.tan((2 * Math.PI) * (rep / 21599));
}
ready
5) Math.asin
for (var rep = 0; rep < 21600; rep++) {
  a = Math.asin((2 * Math.PI) * (rep / 21599));
}
ready

Revisions

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