Comparing custom and bult-in Math.round

JavaScript performance comparison

Revision 2 of this test case created

Info

See: http://stackoverflow.com/q/8483357/1011582

Custom Math.round implementation is:

R1(a,b){var c=a%b;return a-c+(c/b+1.5>>1)*b}

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function R1(a,b){var c=a%b;return a-c+(c/b+1.5>>1)*b}
    R2 = Math.round;
    function R3(a){return Math.round(a)}
   
    tally = 0
};
</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
Custom Math.round
tally += R1(tally,1);
pending…
Built-in Math.round (direct)
tally += R2(tally);
pending…
Built-in Math.round (wrapped)
tally += R3(tally);
pending…
Custom Math.round #OPT
R1(3.5,1)
pending…
Built-in Math.round (direct) #OPT
R2(3.5)
pending…
Built-in Math.round (wrapped) #OPT
R3(3.5)
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