<!--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-->
var a = 0.1;
var b = 0.2;
((a + b) * b).toString();
var a = 0.1;
var b = 0.2;
((a + b) * b).toFixed(9);
var a = new Decimal("0.1");
var b = new Decimal("0.2");
a.plus(b).times(b).toString();
var a = new Big("0.1");
var b = new Big("0.2");
a.plus(b).mul(b).toString();
var a = new BigNumber("0.1");
var b = new BigNumber("0.2");
a.plus(b).multipliedBy(b).toString();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native | |
Native + toFixed | |
decimal.js | |
big.js | |
bignumber.js |
Test name | Executions per second |
---|---|
Native | 188134320.0 Ops/sec |
Native + toFixed | 10307841.0 Ops/sec |
decimal.js | 746503.0 Ops/sec |
big.js | 1245205.2 Ops/sec |
bignumber.js | 650030.6 Ops/sec |
Let's dive into the explanation of the benchmark.
Benchmark Overview
The benchmark measures the performance of three libraries for arbitrary-precision arithmetic: big.js
, decimal.js
, and bignumber.js
. The benchmarks compare the execution speed of these libraries when performing basic arithmetic operations, such as addition, multiplication, and string conversion. There are two variations in each library:
toFixed
method, which can affect performanceLibrary Overview
Options Compared
The benchmark compares the execution speed of each library with the following options:
toFixed
method, which can affect performancePros and Cons
Other Considerations
When choosing an arbitrary-precision arithmetic library, consider the specific requirements of your project:
Alternative Libraries
If you're looking for alternative libraries, consider:
Keep in mind that each library has its strengths and weaknesses, so it's essential to evaluate your specific requirements before choosing a library.