Math.round vs hack

JavaScript performance comparison

Revision 94 of this test case created by afaf

Preparation code

<script>
  var somenum = -500 + (Math.random() * 1000);
  var rounded;
</script>
      
<script>
Benchmark.prototype.setup = function() {
  function round(a){
    var b=a|0;
    return a-b<.5?b:b+1;
  }

};
</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
"proper" rounding
Math.round(4.6253256533);
Math.round(19345.361612);
pending…
Hack rounding
round(4.6253256533);
round(19345.361612);
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