Round to Nearest

JavaScript performance comparison

Test case created by Phrogz

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function roundToNearest1( number, multiple ){
    return Math.round(number/multiple)*multiple;
  }
  
  function roundToNearest2( number, multiple ){
    return number+(multiple/2) - (number+(multiple/2)) % multiple;
  }
  
  function roundToNearest3( number, multiple ){
    var n = multiple/2;
    return number+n - (number+n) % multiple;
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Math.round
for (var i=-100;i<100;++i) roundToNearest1(i,20);
pending…
Mod with inline rounding
for (var i=-100;i<100;++i) roundToNearest2(i,20);
pending…
Mod with cached rounding
for (var i=-100;i<100;++i) roundToNearest3(i,20);
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.

0 Comments