Bit compressing in JavaScript I have an bit array var data = []; ... and I have the following function:

jsPerf

function getBit(n) {
    return (data[~~(n / 32)] &gt;&gt; (n % 32)) &amp; 1;
}


Because this is a bottleneck, I need the fastest cross-browser solution in my code, can anybody help make it any faster?

Also, ~~(n / 32) === Math.floor(n / 32)

It can be algorithm optimization or syntax optimization (such as asm.js) or something else.  Should I change the array type (typedArray or similar)?
I tried to speed up your formula :     

return (data[n &gt;&gt; 5] &gt;&gt; (n &amp; 31)) &amp; 1;


.... All results are very close anyway : jsperf.com/fastest-bit-compressing/7. 

• notice that you can inline the function by yourself (replace function call by direct computation).

• you might wan to cache latest array access by yourself during your computations.

• Or you might want to do the caching in the function. Efficiency will depends on the 'randomness' of your
use of the bits.   

function getBit(n) {
    var itemIndex = n &gt;&gt; 5;
    if (itemIndex != lastItemIndex ) {
        lastItem = data[itemIndex];
        lastItemIndex = itemIndex ;
    }
    return lastItem &gt;&gt; (n &amp; 31)) &amp; 1;
}
var lastItemIndex = -1, lastItem = 0;