Math.floor vs Math.round vs parseInt vs Bitwise

JavaScript performance comparison

Revision 103 of this test case created by Rob Adams

Info

String-to-Int in all form possible

Preparation code

<script>
var num = String(Math.random() * 255);
function x(e) { return Math.floor(e)|0; }
</script>
<script>
Benchmark.prototype.teardown = function() {
    num = String(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 >> 0;
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