<<0 faster than ~~ at least in chrome 10.0.648.15 (parseInt alternatives)

Was reading about use of ~~ in javascript http://james.padolsey.com/javascript/double-bitwise-not and saw the comment from Lea about using <<0 to achieve the same, so I thought it would be interesting to compare the performance of both.

In my tests which tested the expression 100 million times for first a positive floating point value, and then again for another 100 million times on a negative floating point value, I got the following results:

~~n did it in 4.107 seconds
n<<0 did it in 4.028 seconds

Test was done in chrome 10.

Update: 2013-02-22.

Since I made this post, I ran some more performance tests using various methods for converting numbers to integers, and basically all methods, with the exception of parseInt(n) (which is hideously slow), are all roughly the same performance.  Here are my results (tests run using nodejs on a 2012 macbook pro):-

T1 1000000000 ops took 977ms for Math.floor(1.5) = 1
T2 1000000000 ops took 981ms for 1.5|0 = 1
T3 1000000000 ops took 984ms for 1.5>>0 = 1
T4 1000000000 ops took 976ms for ~~1.5 = 1
T5 1000000000 ops took 15131ms for parseInt(1.5) = 1

T1 1000000000 ops took 976ms for Math.floor(-1.5) = -2
T2 1000000000 ops took 978ms for -1.5|0 = -1
T3 1000000000 ops took 976ms for -1.5>>0 = -1
T4 1000000000 ops took 984ms for ~~-1.5 = -1
T5 1000000000 ops took 16939ms for parseInt(-1.5) = -1 

So any of the bitwise methods of converting to integer will do the job that parseInt() will do at a fraction of the cost.  In my code I have started using |0 as its simple an neat.

The code I used to run the test:-

(function(console) {
    var run_test = function(v,l) {
        var t1 = (function() { var s = new Date(), n = 0; while (n < l) { Math.floor(v); n++; } return (new Date()) - s; })();
        console.log("T1 " + l + " ops took " + t1 + "ms for Math.floor("+v+") =" + Math.floor(v));
        var t2 = (function() { var s = new Date(), n = 0; while (n < l) { v|0; n++; } return (new Date()) - s; })();
        console.log("T2 " + l + " ops took " + t2 + "ms for "+v+"|0 = " + (v|0));
        var t3 = (function() { var s = new Date(), n = 0; while (n < l) { v >> 0; n++; } return (new Date()) - s; })();
        console.log("T3 " + l + " ops took " + t3 + "ms for "+v+">>0 = " + (v>>0));
        var t4 = (function() { var s = new Date(), n = 0; while (n < l) { ~~v; n++; } return (new Date()) - s; })();
        console.log("T4 " + l + " ops took " + t4 + "ms for ~~"+v+" = " + (~~v));
        var t5 = (function() { var s = new Date(), n = 0; while (n < l) { parseInt(v); n++; } return (new Date()) - s; })();
        console.log("T5 " + l + " ops took " + t5 + "ms for parseInt("+v+") = " + parseInt(v));
    };
    run_test(1.5,1000000000);
    run_test(-1.5,1000000000);
})(console);
Advertisements

About austinfrance

Technical Developer @ RedSky IT / Explorer Software
This entry was posted in JavaScript and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s