<!--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.2/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.110000000000000000001;
var b = 0.210000000000000000001;
((a + b) * b).toString();
var a = 0.110000000000000000001;
var b = 0.210000000000000000001;
((a + b) * b).toFixed(9);
var a = new Decimal("0.110000000000000000001");
var b = new Decimal("0.210000000000000000001");
a.plus(b).times(b).toString();
var a = new Big("0.110000000000000000001");
var b = new Big("0.210000000000000000001");
a.plus(b).mul(b).toString();
var a = new BigNumber("0.110000000000000000001");
var b = new BigNumber("0.210000000000000000001");
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 | 96244864.0 Ops/sec |
Native + toFixed | 6800097.5 Ops/sec |
decimal.js | 431013.8 Ops/sec |
big.js | 183435.1 Ops/sec |
bignumber.js | 426145.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Overview
The provided benchmark is designed to compare the performance of different libraries for working with large numbers: bignumber.js
, big.js
, and decimal.js
. The benchmark consists of multiple test cases that aim to measure the speed of various operations, such as addition, multiplication, and string conversion.
Libraries and their purpose
decimal.js
: A library for precise decimal arithmetic. It provides a Decimal
class that allows you to work with numbers with high precision (up to 29 digits).big.js
: A library for large integers and decimals. It provides a Big
class that allows you to work with arbitrary-precision integers.bignumber.js
: A library for big number arithmetic. It provides a BigNumber
class that allows you to work with arbitrarily large numbers.Options compared
The benchmark compares the performance of each library when performing different operations:
Native
: Uses JavaScript's built-in types (e.g., number
) instead of a dedicated library.Native + toFixed
: Uses JavaScript's built-in types and adds the toFixed()
method for string conversion.Decimal
, Big
, BigNumber
) for arithmetic operations.Pros and Cons
bignumber.js
: Pros:big.js
: Pros:decimal.js
.decimal.js
: Pros:Other considerations
Alternatives
If you're looking for alternatives, consider:
Fraction.js
: A library for rational numbers (fractions) that might be suitable for specific use cases.jsbn
: A small, lightweight library for arbitrary-precision integers.BigInt
: JavaScript's built-in support for arbitrary-precision integers in ECMAScript 2020 and later versions.Keep in mind that the choice of library depends on your specific requirements, performance constraints, and code complexity considerations.