<!--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 = 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 |
---|---|
decimal.js | |
big.js | |
bignumber.js |
Test name | Executions per second |
---|---|
decimal.js | 581611.1 Ops/sec |
big.js | 993168.8 Ops/sec |
bignumber.js | 546844.8 Ops/sec |
I'll break down the provided JSON and explain what's being tested, compared, and their pros/cons.
Benchmark Overview
The benchmark is comparing three libraries: big.js
, decimal.js
, and bignumber.js
. These libraries are designed to handle large numbers and decimal calculations in JavaScript. The test focuses on measuring the performance of each library when performing a specific calculation: (a + b) * b
(where a
and b
are two decimal values).
Library Overview
Test Cases
Each test case is a small script that uses one of these libraries to perform the same calculation. The differences lie in how the calculation is expressed:
decimal.js
:var a = new Decimal("0.1");
var b = new Decimal("0.2");
a.plus(b).times(b).toString();
This test case creates two decimal numbers using Decimal()
and performs arithmetic operations on them.
big.js
:var a = new Big("0.1");
var b = new Big("0.2");
a.plus(b).mul(b).toString();
This test case uses the Big()
constructor to create two large numbers and performs arithmetic operations using the plus()
and mul()
methods.
bignumber.js
:var a = new BigNumber("0.1");
var b = new BigNumber("0.2");
a.plus(b).multipliedBy(b).toString();
This test case uses the BigNumber()
constructor to create two large numbers and performs arithmetic operations using the plus()
and multipliedBy()
methods.
Pros/Cons of each approach
big.js
, might require more memory usage due to extra data structures.big.js
, relies on caching mechanism which can lead to performance issues if not properly configured.Device-specific considerations
The benchmark is run in a Chrome 96 browser on Windows Desktop devices. The results may vary across different operating systems, browsers, and hardware configurations.
Alternatives
Other libraries that might be used for large number arithmetic include:
Please note that the suitability of each library depends on the specific requirements and constraints of your project.