<!--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 d = a > b || a < b;
const a = new Decimal('123.456');
const b = new Decimal('123.456');
const d = a.greaterThan(b) || a.lessThan(b);
const a = new Big('123.456');
const b = new Big('123.456');
const d = a.gt(b) || a.lt(b);
var a = new BigNumber('123.456');
var b = new BigNumber('123.456');
const d = a.gt(b) || a.lt(b);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
native | |
decimal.js | |
big.js | |
bignumber.js |
Test name | Executions per second |
---|---|
native | 10257499.0 Ops/sec |
decimal.js | 1075698.5 Ops/sec |
big.js | 1544318.5 Ops/sec |
bignumber.js | 903064.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is being tested?
The benchmark measures the performance of different libraries for working with numbers in JavaScript:
Number
(native JavaScript type)Big.js
Bignumber.js
Decimal.js
Specifically, the tests compare the performance of these libraries when performing arithmetic operations on floating-point numbers.
Options compared:
The benchmark compares four options:
Number
typeBig.js
Bignumber.js
Decimal.js
Each library has its own strengths and weaknesses, which we'll discuss next.
Pros and Cons of each approach:
Number
type:Number
type.Number
type.Libraries used in the tests:
Big.js
: A library for fast and precise arithmetic with large numbers.Bignumber.js
: A library for performing mathematical operations on very large and very small numbers.Decimal.js
: A library for performing accurate calculations with decimal numbers.Special JS features or syntax:
None mentioned in the benchmark definition. The tests use standard JavaScript syntax for arithmetic operations.
Other considerations:
When choosing a library, consider factors such as:
Alternative approaches:
If none of these libraries suit your specific use case, you may want to explore other options, such as:
Fraction.js
: A library for performing arithmetic with fractions.Keep in mind that the choice of library ultimately depends on your specific requirements and constraints.