Bits to bytes (v2)

Revision 2 of this benchmark created on


Description

Testing if naive parseInt is faster than other things that come to mind.

Setup

var bits = [0, 0, 1, 1, 0, 0, 1, 0]; 

var makeByteR = (b) => b.reduceRight((c, bi, i) => (bi ? c | (1 << 7-i) : c), 0);

var makeByteF = (b, c = 0, i) => {
  for(i = 0; i<8; i++){
  	if(b[7-i]){
  	  c |= 1 << i ;
    }
  } 
  return c; 
} 

var makeByteP = (b, c = 0) => {
  while(b.length)c = b.pop() ? c | 1 << 7 - b.length: c;
  return c;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Join + parseInt
var byte = parseInt(bits.join(''), 2);
ready
reduce
var byte = makeByteR(bits);
ready
for loop
var byte = makeByteF(bits);
ready
while+pop
var byte = makeByteP([...bits]);
ready

Revisions

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