<!--script src="https://raw.githubusercontent.com/iriscouch/bigdecimal.js/v0.6.1/lib/bigdecimal.js"></script-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/9.0.1/bignumber.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/big.js/6.0.3/big.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/10.2.1/decimal.min.js"></script>
<!--script src="https://raw.githubusercontent.com/infusion/Fraction.js/v4.0.12/fraction.min.js"></script-->
const a = parseFloat('123.456');
const b = parseFloat('123.456');
//const c = isNan(a) && isNan(b);
const d = a > b || a < b;
const e = (a + b) * b;
const a = new Decimal('123.456');
const b = new Decimal('123.456');
//const c = a.isNan() && b.isNan();
const d = a.greaterThan(b) || a.lessThan(b);
const e = a.plus(b).times(b);
const a = new Big('123.456');
const b = new Big('123.456');
//const c = a.isNan() && b.isNan();
const d = a.gt(b) || a.lt(b);
const e = a.plus(b).mul(b);
var a = new BigNumber('123.456');
var b = new BigNumber('123.456');
//const c = a.isNan() && b.isNan();
const d = a.gt(b) || a.lt(b);
const e = a.plus(b).multipliedBy(b);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
native | |
decimal.js | |
big.js | |
bignumber.js |
Test name | Executions per second |
---|---|
native | 4462833.5 Ops/sec |
decimal.js | 683490.8 Ops/sec |
big.js | 1042611.1 Ops/sec |
bignumber.js | 653750.8 Ops/sec |
Measuring the performance of numerical calculations in JavaScript is an interesting task.
What's being tested?
The provided benchmark tests four different approaches to perform numerical calculations:
Options compared
Each test case compares the performance of different numerical libraries or approaches:
decimal.js
vs big.js
: Both libraries provide support for decimal arithmetic, but with slightly different implementations and optimizations.bignumber.js
vs big.js
: Both libraries provide support for arbitrary-precision arithmetic, but with different binary formats and optimization strategies.Pros and cons
Here's a brief summary of the pros and cons of each approach:
Other considerations
When choosing a numerical library or approach, consider the following factors:
Alternatives
If you're looking for alternative libraries or approaches, consider:
arbitrary-precision
, provide built-in support for arbitrary-precision arithmetic.Note: The choice of numerical library or approach ultimately depends on your specific use case, performance requirements, and developer preferences.