var bigInt1 = 1000000n
var bigInt2 = 50n
var number1 = 1000000
var number2 = 50
a = number1
b = number2
c = a - b
c = a * b
c = a + b
c = a / b
a = bigInt1
b = bigInt2
c = a - b
c = a * b
c = a + b
c = a / b
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Number | |
BigInt |
Test name | Executions per second |
---|---|
Number | 2362807.5 Ops/sec |
BigInt | 2244147.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
What is being tested?
MeasureThat.net is testing the performance of two types of numbers in JavaScript: Number
and BigInt
. Specifically, it's comparing the performance of arithmetic operations (addition, subtraction, multiplication, and division) on both types of numbers.
Options compared
The benchmark compares the performance of Number
and BigInt
for the following arithmetic operations:
a + b
)a - b
)a * b
)a / b
)Pros and cons of each approach
Number
Pros:
Cons:
BigInt
Pros:
Cons:
Number
Library usage
In the benchmark, the BigInt
library is used for its implementation. Specifically, it's using the n
syntax introduced in ECMAScript 2020 to represent large integers.
For those unfamiliar with n
, it represents a BigInt literal, allowing for arbitrary-precision arithmetic. This syntax was added to provide support for cryptographic primitives and other use cases requiring precise decimal representation.
Special JS feature or syntax
The benchmark uses the n
syntax to represent large integers using BigInt
. This is a relatively recent addition to JavaScript (introduced in ECMAScript 2020) and provides a convenient way to work with arbitrary-precision numbers.
Other alternatives
If you're working with very large numbers or require precise control over decimal representation, other alternatives might include:
jsbn
or decimal.js
, which provide support for arbitrary-precision arithmetic in JavaScript.crypto-js
or big-integer
, which offer a range of cryptographic primitives and functions.However, keep in mind that these alternatives may have different performance characteristics, compatibility issues, or additional complexity compared to the built-in BigInt
implementation in modern browsers and JavaScript engines.