<!--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 | 693826.1 Ops/sec |
big.js | 1271232.1 Ops/sec |
bignumber.js | 676916.6 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, compared options, pros and cons of those approaches, and other considerations.
Benchmark Overview
The benchmark compares the performance of three libraries: bignumber.js
, big.js
, and decimal.js
. The test cases are designed to measure the execution time of specific arithmetic operations using each library.
Test Cases
There are three individual test cases:
decimal.js
: Measures the performance of decimal arithmetic using the Decimal
class.var a = new Decimal("0.1"); var b = new Decimal("0.2"); a.plus(b).times(b).toString();
big.js
: Measures the performance of big integer arithmetic using the Big
class.var a = new Big("0.1"); var b = new Big("0.2"); a.plus(b).mul(b).toString();
bignumber.js
: Measures the performance of big number arithmetic using the BigNumber
class.var a = new BigNumber("0.1"); var b = new BigNumber("0.2"); a.plus(b).multipliedBy(b).toString();
Comparison of Options
The three libraries are compared in terms of their performance, precision, and usability.
bignumber.js
is expected to be faster than the other two due to its optimized JavaScript implementation.decimal.js
: Designed for precise decimal arithmetic, but may not be suitable for very large numbers.big.js
: Provides a simple and intuitive API, but may not offer the same level of performance as bignumber.js
.bignumber.js
: Offers high performance and a robust feature set, making it a popular choice for big number arithmetic.Pros and Cons
decimal.js
:bignumber.js
.big.js
:bignumber.js
, limited features compared to other libraries.bignumber.js
:Other Considerations
decimal.js
. Its inclusion would further complicate the comparison.toFixed()
is also included in the benchmark. This method uses the browser's native arithmetic implementation and may not offer the same level of performance or precision as the libraries.Alternatives
If you're looking for alternatives to these libraries, consider:
Big.js
: A JavaScript implementation of the Big Number library.decimal.js
: Another popular decimal arithmetic library.js-bigint
: A lightweight JavaScript implementation of arbitrary-precision integers.toFixed()
: For simple cases where precision and performance are not critical.Ultimately, the choice of library depends on your specific requirements, such as precision, performance, or ease of use.