<script src="https://cdn.jsdelivr.net/npm/bigdecimal@0.6.1/lib/bigdecimal.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bignumber.js@9.0.1/bignumber.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/big.js@6.0.3/big.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/decimal.js@10.2.1/decimal.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fraction.js@4.0.13/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();
var a = new BigDecimal("0.1");
var b = new BigDecimal("0.2");
a.add(b).multiply(b).toString();
var a = new Fraction("0.1");
var b = new Fraction("0.2");
a.add(b).mul(b).toString();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
decimal.js | |
big.js | |
bignumber.js | |
bigdecimal.js | |
Fraction.js |
Test name | Executions per second |
---|---|
decimal.js | 604702.9 Ops/sec |
big.js | 1043072.2 Ops/sec |
bignumber.js | 633461.9 Ops/sec |
bigdecimal.js | 133575.4 Ops/sec |
Fraction.js | 1197981.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is being tested?
MeasureThat.net is testing various libraries that provide support for decimal arithmetic, specifically:
bignumber.js
: A library for high-precision floating-point numbers.big.js
: A library that provides support for decimal arithmetic with a focus on simplicity and ease of use.decimal.js
: A widely-used library for working with decimal numbers in JavaScript.Fraction.js
(without native): A library for working with rational numbers.Options compared
The tests compare the performance of each library when performing basic arithmetic operations, such as addition, multiplication, and concatenation, on two decimal numbers:
var a = new Decimal("0.1"); var b = new Decimal("0.2");
var a = new Big("0.1"); var b = new Big("0.2");
var a = new BigNumber("0.1"); var b = new BigNumber("0.2");
var a = new BigDecimal("0.1"); var b = new BigDecimal("0.2");
var a = new Fraction("0.1"); var b = new Fraction("0.2");
Pros and cons of each approach
Here's a brief summary of the pros and cons of each library:
Special JS features
None of the libraries mentioned in the benchmark require any special JavaScript features or syntax beyond standard arithmetic operations.
Other alternatives
If you're looking for alternative libraries that provide similar functionality, consider:
Keep in mind that each library has its strengths and weaknesses, and the choice ultimately depends on your specific use case and requirements.
I hope this explanation helps you understand what's being tested in MeasureThat.net!