Math.floor vs Math.round vs parseInt vs Bitwise (v126)

Revision 126 of this benchmark created by PAEz on


Description

Rounding in all form possible and not-not

PAEz: All the previous tests I looked at had hard coded values that dont change....this makes me worry that it'll get optimized, so I added in something to try and stop that.....seems to kill Math.round in Chrome, wonder why?

Setup

var numbers=[];
    var numbersCount=5000;
    for (var i=0,iend=numbersCount;i<iend;i++) numbers.push(Math.random*i);

Test runner

Ready to run.

Testing in
TestOps/sec
Math.floor
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += Math.floor(numbers[i]);
}
ready
Math.round
for (var i=0;i<numbersCount;i++){
var z = Math.random();
 z += Math.round(numbers[i]);
}
ready
parseInt
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += parseInt(numbers[i]);
}
ready
Bitwise |
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += numbers[i] | 0;
}
ready
Bitwise
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += numbers[i] >> 0;
}
ready
Not not
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += ~~(numbers[i]);
}
ready
Math.round Equivalent
for (var i=0;i<numbersCount;i++){
var z = Math.random();
z += (numbers[i]+0.5) | 0;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.