Math.floor vs Math.round vs parseInt vs Bitwise vs Tilde

JavaScript performance comparison

Revision 35 of this test case created

Info

Rounding in all form possible

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var round = Math.round;
    var floor = Math.floor;
    var number_of_randoms = 10000;
    var len = number_of_randoms;
    var values = [];
    var shuffles = [];
    var index = 0;
    var results = 0;
   
    // Fill arrays
    while(len--){
    values.push([
    Math.random()*number_of_randoms+1,  
    Math.random()*number_of_randoms+1,
    Math.random()*number_of_randoms+1,
    Math.random()*number_of_randoms+1,
    Math.random()*number_of_randoms+1,
    Math.random()*number_of_randoms+1,
    Math.random()*number_of_randoms+1,
    Math.random()*number_of_randoms+1,
    Math.random()*number_of_randoms+1
    ]);
    shuffles.push(len);
    }
    len = number_of_randoms;
   
    // Shuffle "shuffles" array
    while(len--){
    index = Math.floor(Math.random()*number_of_randoms);
    results = shuffles[len];
    shuffles[len] = shuffles[index];
    shuffles[index] = results;
    }
    results = 0;
    index = Math.floor(Math.random()*number_of_randoms);
};

Benchmark.prototype.teardown = function() {
    results = 0;
    index = Math.floor(Math.random()*number_of_randoms);
};
</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
Tilde
results += ~~(values[index][0] / values[index][1]);
results += ~~(values[index][1] / values[index][2]);
results += ~~(values[index][2] / values[index][0]);
results += ~~(values[index][3] / values[index][4]);
results += ~~(values[index][3] / values[index][5]);
results += ~~(values[index][5] / values[index][3]);
results += ~~(values[index][6] / values[index][7]);
results += ~~(values[index][7] / values[index][8]);
results += ~~(values[index][8] / values[index][6]);
index =  shuffles[index];
pending…
Math.round
results += Math.round(values[index][0] / values[index][1]);
results += Math.round(values[index][1] / values[index][2]);
results += Math.round(values[index][2] / values[index][0]);
results += Math.round(values[index][3] / values[index][4]);
results += Math.round(values[index][3] / values[index][5]);
results += Math.round(values[index][5] / values[index][3]);
results += Math.round(values[index][6] / values[index][7]);
results += Math.round(values[index][7] / values[index][8]);
results += Math.round(values[index][8] / values[index][6]);
index =  shuffles[index];
pending…
parseInt
results += parseInt(values[index][0] / values[index][1]);
results += parseInt(values[index][1] / values[index][2]);
results += parseInt(values[index][2] / values[index][0]);
results += parseInt(values[index][3] / values[index][4]);
results += parseInt(values[index][3] / values[index][5]);
results += parseInt(values[index][5] / values[index][3]);
results += parseInt(values[index][6] / values[index][7]);
results += parseInt(values[index][7] / values[index][8]);
results += parseInt(values[index][8] / values[index][6]);
index =  shuffles[index];
pending…
Bitwise |
results += values[index][0] / values[index][1] | 0;
results += values[index][1] / values[index][2] | 0;
results += values[index][2] / values[index][0] | 0;
results += values[index][3] / values[index][4] | 0;
results += values[index][3] / values[index][5] | 0;
results += values[index][5] / values[index][3] | 0;
results += values[index][6] / values[index][7] | 0;
results += values[index][7] / values[index][8] | 0;
results += values[index][8] / values[index][6] | 0;
index =  shuffles[index];
pending…
Bitwise
results += values[index][0] / values[index][1] >> 0;
results += values[index][1] / values[index][2] >> 0;
results += values[index][2] / values[index][0] >> 0;
results += values[index][3] / values[index][4] >> 0;
results += values[index][3] / values[index][5] >> 0;
results += values[index][5] / values[index][3] >> 0;
results += values[index][6] / values[index][7] >> 0;
results += values[index][7] / values[index][8] >> 0;
results += values[index][8] / values[index][6] >> 0;
index =  shuffles[index];
pending…
Math.floor
results += Math.floor(values[index][0] / values[index][1]);
results += Math.floor(values[index][1] / values[index][2]);
results += Math.floor(values[index][2] / values[index][0]);
results += Math.floor(values[index][3] / values[index][4]);
results += Math.floor(values[index][3] / values[index][5]);
results += Math.floor(values[index][5] / values[index][3]);
results += Math.floor(values[index][6] / values[index][7]);
results += Math.floor(values[index][7] / values[index][8]);
results += Math.floor(values[index][8] / values[index][6]);
index =  shuffles[index];
pending…
round
results += round(values[index][0] / values[index][1]);
results += round(values[index][1] / values[index][2]);
results += round(values[index][2] / values[index][0]);
results += round(values[index][3] / values[index][4]);
results += round(values[index][3] / values[index][5]);
results += round(values[index][5] / values[index][3]);
results += round(values[index][6] / values[index][7]);
results += round(values[index][7] / values[index][8]);
results += round(values[index][8] / values[index][6]);
index =  shuffles[index];
pending…
floor
results += floor(values[index][0] / values[index][1]);
results += floor(values[index][1] / values[index][2]);
results += floor(values[index][2] / values[index][0]);
results += floor(values[index][3] / values[index][4]);
results += floor(values[index][3] / values[index][5]);
results += floor(values[index][5] / values[index][3]);
results += floor(values[index][6] / values[index][7]);
results += floor(values[index][7] / values[index][8]);
results += floor(values[index][8] / values[index][6]);
index =  shuffles[index];
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