Math.floor vs Math.round vs parseInt vs Bitwise

JavaScript performance comparison

Revision 126 of this test case created by PAEz

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var numbers=[];
  var numbersCount=5000;
  for (var i=0,iend=numbersCount;i<iend;i++) numbers.push(Math.random*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
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += Math.floor(numbers[i]);
}
pending…
Math.round
for (var i=0;i<numbersCount;i++){
var z = Math.random();
 z += Math.round(numbers[i]);
}
pending…
parseInt
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += parseInt(numbers[i]);
}
pending…
Bitwise |
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += numbers[i] | 0;
}
pending…
Bitwise
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += numbers[i] >> 0;
}
pending…
Not not
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += ~~(numbers[i]);
}
pending…
Math.round Equivalent
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += (numbers[i]+0.5) | 0;
}
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

PAEz (revision owner) commented :

You still suck IE!!!!!!!!!!!!!!!!!!!