Math.floor vs Math.round vs parseInt vs Bitwise

JavaScript performance comparison

Revision 100 of this test case created

Info

Rounding in all form possible

Preparation code

<script>
var num = Math.random() * 255;
function x(e) { return Math.floor(e)|0; }
</script>
<script>
Benchmark.prototype.teardown = function() {
    num = Math.random() * 255;
};
</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
Math.floor
Math.floor(num);
pending…
Math.round
Math.round(num);
pending…
parseInt
parseInt(num,10);
pending…
Bitwise |
num | 0;
pending…
Bitwise
num >> 1;
pending…
typed function
x(num);
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