# get individual digits of

## Description

"what are the decimal digits of π β β" "figure it out with Math.ceil(Math.log(π)) or String(π).length" "which is...... faster" c.f. https://jsperf.app/gorehi for just getting the length itself

## Setup

``````// only works if JS doesn't sciencify the string
// representation for large values (X.XXXe+X)
// so like... π < 10Β²ΒΉ or so?
const L = 10e20;
function test (digitsOfN) {
let π = 100;
while ((π *= 1.19) < L) digitsOfN(π);
}``````

## Test runner

Ready to run.

Testing in
TestOps/sec
get the digits of π using πΈπππ β
``````// goes left to right shaving off the leading digit
test(n => {
let l = Math.ceil(Math.log10(n))
const d = [];
while (l--) {
const m = (10**l);
d.push(Math.trunc(n / m))
n = n % m;
}
return d;
});
``````
ready
get the digits of π using π’π½π»π²π·π°
``test(n => Array.from(String(n), Number));``
ready
get the digits of π using πΈπππ β‘
``````// goes right to left shaving off the last digit
test(n => {
const d = [];
while (n) {
const l = n % 10;
n = (n - l) / 10
d.unshift(l);
}
return d;
});``````
ready
get the digits of π using πΈπππ β’
``````// goes right to left shaving off the last digit
// (assembles in reverse then flips the array)
test(n => {
const d = [];
while (n) {
const l = n % 10;
n = (n - l) / 10
d.push(l);
}
return d.reverse();
});``````
ready
get the digits of π using πΈπππ β£
``````// goes right to left shaving off the last digit
// (trying optimizations)
test(n => {
const d = [];
while (n) {
d.unshift(n % 10);
n = Math.trunc(n / 10)

}
return d;
});``````
ready
get the digits of π using πΈπππ β€
``````// goes right to left shaving off the last digit
// (assembles in reverse then flips the array)
// (trying optimizations)
test(n => {
const d = [];
while (n) {
d.push(n % 10);
n = Math.trunc(n / 10)
}
return d.reverse();
});
``````
ready

## Revisions

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