<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)).toString();
}
{
const a = Math.random();
const b = Math.random();
const c = Math.random();
const d = Math.random();
((a + b) * (c - d)).toFixed(9);
}
{
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)).toString();
}
{
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)).toFixed(18);
}
{
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)).toString();
}
{
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() | 2022916.0 Ops/sec |
Native toFixed(9) | 2319269.0 Ops/sec |
bignumber.js toString() | 220101.3 Ops/sec |
bignumber.js toFixed(18) | 201508.3 Ops/sec |
big.js toString() | 230092.5 Ops/sec |
big.js toFixed(18) | 235953.5 Ops/sec |
decimal.js toString() | 190278.5 Ops/sec |
decimal.js toFixed(18) | 175009.4 Ops/sec |
The answer is not explicitly stated in the provided information, but based on the benchmark results, it appears that the performance of different libraries for decimal arithmetic and formatting vary across different browsers and devices.
However, if we look at the test names and the corresponding benchmark results, we can see that:
Native toFixed(9)
has a high number of executions per second (2319269.0), indicating fast performance.Native toString()
also has a high number of executions per second (2022916.0), suggesting good performance for string conversion.big.js
and bignumber.js
having slightly higher rates than decimal.js
.Based on this information, it's difficult to make a definitive conclusion about which library is the fastest or most performant. However, it's clear that the native JavaScript methods are among the fastest, followed by the other libraries.
If you're looking for a recommendation, I would suggest using the native JavaScript methods (e.g., toFixed(9)
) when possible, as they tend to be the fastest and most efficient. If you need to use a library for decimal arithmetic and formatting, consider using big.js
or bignumber.js
, which appear to have better performance than decimal.js
.