Divide or modulo

JavaScript performance comparison

Test case created

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.3/underscore-min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    negativeDiv = function(chanceOfNegative) {
      if (Math.random() > 1 / (chanceOfNegative || 2)) {
        return 1
      } else {
        return -1
      }
    }
   
    negativeMod = function(chanceOfNegative) {
      if (_.random(100) % (chanceOfNegative || 2) !== 0) {
        return 1
      } else {
        return -1
      }
    }
};
</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
divide
_(10).times(function(n) { negativeDiv(n) })
 
pending…
modulo
_(10).times(function(n) { negativeMod(n) })
 
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