Math.floor, parseInt, left shift and binary OR

JavaScript performance comparison

Revision 4 of this test case created by Dj Gilcrease

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  i = Math.random() * 10000;

};

Benchmark.prototype.teardown = function() {
  delete i;

};
</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(i);
pending…
parseInt()
parseInt(i);
pending…
left shift
i << 0;
pending…
binary OR
0|i
pending…
Tilde
~~i
pending…
function call overhead
(function(x){return 0|x;}(i))
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

Dj Gilcrease commented :

This revision is just to show that trying to optimize to this level is basically pointless as your operation is foing to be totaly dominated by the function call overhead. The loss of readability for the few peco seconds of benefit is not worth the maitnence hastle 6 months from now when you have to re look at code you optimized too early.