<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/9.1.0/bignumber.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/big.js/6.2.1/big.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/10.3.1/decimal.min.js"></script>
<!--<script src="https://cdn.jsdelivr.net/npm/decimal.js-light@2.5.1/decimal.min.js"></script>-->
BigNumber.set({
EXPONENTIAL_AT: 999,
DECIMAL_PLACES: 18
});
Decimal.set({
toExpNeg: -999,
toExpPos: 999,
precision: 18
});
Big.DP = 18;
Big.NE = -999;
Big.PE = 999;
{
const a = Math.random();
const b = Math.random();
const c = Math.random();
const d = Math.random();
((a + b) * (c - d));
}
{
const a = Math.random();
const b = Math.random();
const c = Math.random();
const d = Math.random();
((a + b) * (c - d));
}
{
const a = BigNumber(Math.random());
const b = BigNumber(Math.random());
const c = BigNumber(Math.random());
const d = BigNumber(Math.random());
a.plus(b).times(c.minus(d));
}
{
const a = BigNumber(Math.random());
const b = BigNumber(Math.random());
const c = BigNumber(Math.random());
const d = BigNumber(Math.random());
a.plus(b).times(c.minus(d));
}
{
const a = Big(Math.random());
const b = Big(Math.random());
const c = Big(Math.random());
const d = Big(Math.random());
a.plus(b).times(c.minus(d));
}
{
const a = Big(Math.random());
const b = Big(Math.random());
const c = Big(Math.random());
const d = Big(Math.random());
a.plus(b).times(c.minus(d)).toFixed(18);
}
{
const a = Decimal(Math.random());
const b = Decimal(Math.random());
const c = Decimal(Math.random());
const d = Decimal(Math.random());
a.plus(b).times(c.minus(d)).toString();
}
{
const a = Decimal(Math.random());
const b = Decimal(Math.random());
const c = Decimal(Math.random());
const d = Decimal(Math.random());
a.plus(b).times(c.minus(d)).toFixed(18);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native toString() | |
Native toFixed(9) | |
bignumber.js toString() | |
bignumber.js toFixed(18) | |
big.js toString() | |
big.js toFixed(18) | |
decimal.js toString() | |
decimal.js toFixed(18) |
Test name | Executions per second |
---|---|
Native toString() | 40250180.0 Ops/sec |
Native toFixed(9) | 40400780.0 Ops/sec |
bignumber.js toString() | 435840.2 Ops/sec |
bignumber.js toFixed(18) | 438181.8 Ops/sec |
big.js toString() | 434633.8 Ops/sec |
big.js toFixed(18) | 379201.7 Ops/sec |
decimal.js toString() | 353112.1 Ops/sec |
decimal.js toFixed(18) | 328715.6 Ops/sec |
Overview of the Benchmark
The benchmark compares the performance of four libraries: big.js
, bignumber.js
, and two implementations of arbitrary-precision arithmetic (native
and decimal.js
). The test is executed on a mobile device running Chrome 126.
Arbitrary-Precision Arithmetic Implementations
There are three implementations:
Key Insights
big.js
and bignumber.js
perform similarly, with slightly slower execution times than the native implementation.decimal.js
is significantly slower than the other implementations.Performance Comparison
The benchmark results show that:
Implementation | Execution Rate |
---|---|
Native | 1050730.25 executions/second |
big.js | 100165.9453125 executions/second |
bignumber.js | 100165.9453125 executions/second |
decimal.js | 79191.1015625 executions/second |
The native implementation outperforms the other libraries, but its accuracy and portability are limited.
Recommendations
Based on these results, if you need to perform arbitrary-precision integer arithmetic in a performance-critical application, using the native JavaScript implementation might be the best choice. However, if you need accurate results across multiple platforms or want a dedicated library for this purpose, big.js
or bignumber.js
could be suitable alternatives.
For decimal arithmetic, decimal.js
is not recommended due to its slow performance.
Keep in mind that these results are specific to the mobile device and Chrome 126 browser. The performance differences may vary on other platforms or browsers.