Bit Counting (v2)

Revision 2 of this benchmark created on


Setup

function countBits_string(n) {
  return n.toString(2).split('1').length - 1;
}

function countBits_arithmetic(n) {
  let count = 0;
  for (let bn = BigInt(n); bn; bn &= bn - 1n) count++;
  return count;
}

const random_int = () => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);

Test runner

Ready to run.

Testing in
TestOps/sec
String Manipulation
countBits_string(random_int());
ready
Arithmetic
countBits_arithmetic(random_int());
ready

Revisions

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