var bigInt1 = 1000000n
var bigInt2 = 50n
var number1 = 1000000
var number2 = 50
a = bigInt1
b = bigInt2
c = a - b
c = a % b
c = a * b
c = a + b
c = a ** b
a = number1
b = number2
c = a - b
c = a % b
c = a * b
c = a + b
c = a ** b
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
BigInt | |
Number |
Test name | Executions per second |
---|---|
BigInt | 1433377.8 Ops/sec |
Number | 37934000.0 Ops/sec |
Let's break down the benchmark and its test cases.
What is being tested?
The provided JSON represents a JavaScript microbenchmark that compares the performance of two data types: Number
and BigInt
. Specifically, it measures the execution time of various arithmetic operations (addition, subtraction, multiplication, division, exponentiation) on both data types for large numbers.
Options compared
Two options are being compared:
Pros and Cons of each approach:
Library usage
The benchmark uses the BigInt
library to create and manipulate BigInt
values. However, since this is a built-in JavaScript feature starting from ECMAScript 2020, no external library is necessary.
Special JS feature or syntax
The test cases utilize the n
suffix for BigInt literals, introduced in ECMAScript 2019. This allows you to create arbitrary-precision integers without needing additional libraries or workarounds.
Other alternatives
If a browser does not support BigInt
, you might consider using:
big.js
or decimal.js
that provide similar arithmetic capabilities.Keep in mind that each alternative has its own trade-offs and requirements, so the choice depends on your specific use case and constraints.