This Javascript program: pi_bigint.js computes hundreds of millions of digits of π using the Chudnovsky series. It assumes that the Javascript engine efficiently computes with large BigInt numbers, which is the case of QuickJS.
Time to compute 100 000 digits of π on a Core i5 4570 CPU at 3.2 GHz:
Size (digits) | QuickJS (2020-01-05) | Spider Monkey (version C70.0a1) | V8 (version 7.7.289) |
---|---|---|---|
100 000 | 0.26 s | 3.6 s | 2.3 s |
Similar programs can be written using the BigFloat (binary floating point) or BigDecimal (decimal floating point) QuickJS extensions: pi_bigfloat.js and pi_bigdecimal.js. Run them with:
qjs --bignum pi_bigfloat.js 1e6Here is the time to compute up to one billion digits of π with QuickJS:
Size (digits) | QuickJS (BigFloat) | QuickJS (BigInt) | QuickJS (BigDecimal) |
---|---|---|---|
100 000 | 0.12 s | 0.26 s | 2.1 s |
1000 000 | 1.4 s | 3.6 s | - |
10 000 000 | 19.1 s | 56.0 s | - |
100 000 000 | 275 s | - | - |
1000 000 000 | 3635 s | - | - |
The BigFloat program is faster because the BigFloat square root is optimized in C while it is implemented in Javascript with a simple algorithm in the BigInt case.
The BigDecimal version is currently much slower because QuickJS does not optimize the operations on large BigDecimal numbers.