integer exponentiation

JavaScript performance comparison

Revision 4 of this test case created by 4esn0k

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var number = 1;
   
    function pow(n, count, acc) {
      return (count < 1 ? acc : (count % 2 === 1 ? pow(n, count - 1, acc * n) : pow(n * n, count / 2, acc)));
    }
   
    function pow_squaring(n, x){
      var r = 1;
      while(x > 1){
        if(x % 2 === 0) { n*=n; x/=2; }
        else { r*=n; x--; }
      }
      return n*r;
    }
};
</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
multiplication
var e = 1024;
while (--e >= 0) {
  pow(number++, e, 1);
}
 
pending…
squaring
var e = 1024;
while (--e >= 0) {
  pow_squaring(number++, e);
}
 
pending…
built-in (Math.pow)
var e = 1024;
while (--e >= 0) {
  Math.pow(number++, e);
}
 
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