getting individual digits of a bigint

Benchmark created on


Description

"how many decimal digits hath 𝑛 ∈ 𝔹𝕀𝔾ℕ" "etc etc"

https://jsperf.app/sutoyu but for bigint

Setup

// test 1!...1000!
function test (fn) {
  let n = 1n;
  for (let i = 1n; i <= 1000n; i++) fn(n *= i);
}

Test runner

Ready to run.

Testing in
TestOps/sec
using string.length
test(n => Array.from(String(n), Number));
ready
using repeated mod-tenning (convert to number in loop)
test(n => {
  const d = [];
  while (n) {
  	d.push(Number(n % 10n));
  	n /= 10n;
  }
  return d.reverse();
});
ready
using repeated mod-tenning (convert to numbers w/ map at the end)
test(n => {
  const d = [];
  while (n) {
  	d.push(n % 10n);
  	n /= 10n;
  }
  return d.reverse().map(Number);
});
ready

Revisions

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