Rounding numbers down

JavaScript performance comparison

Revision 3 of this test case created by and last updated

Preparation code

<script>
  var n = Math.PI, // 3.141592653589793
      substr = String.prototype.substr,
      split = String.prototype.split,
      indexOf = String.prototype.indexOf;
</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.floor
Math.floor(n); // 3
pending…
parseInt
parseInt(n, 10); // 3
pending…
Double bitwise NOT
~~n; // 3
pending…
Bitwise OR
n | n; // 3
pending…
Bitwise OR with 0
n | 0; // 3
pending…
Bitwise AND
n & n; // 3
pending…
Bitwise left shift
n << 0; // 3
pending…
toString, split, and toNumber
('' + n).split('.')[0] * 1; // 3
pending…
String.prototype.substr and indexOf with conversion
+String.prototype.substr.call(n, 0, String.prototype.indexOf.call(n, '.', 0)); // 3
pending…
Cached String.prototype.substr
+substr.call(n, 0, indexOf.call(n, '.', 0)); // 3
pending…
String.prototype.split
+String.prototype.split.call(n, '.')[0]; // 3
pending…
Cached String.prototype.split
+split.call(n, '.')[0]; // 3
pending…
Bitwise right shift
n >> 0; // 3
pending…
Bitwise zero-fill right shift
n >>> 0; // 3
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.

1 Comment

Eric commented :

I think the test that was done with Chrome 9.0.593 is a fraud.