var bigInt1 = 1000000n
var bigInt2 = 5000000n
var number1 = 1000000
var number2 = 5000000
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 | 0.0 Ops/sec |
Number | 18624706.0 Ops/sec |
Let's break down the provided benchmark.
What is being tested?
The benchmark compares the performance of two data types in JavaScript: Number
and BigInt
. Specifically, it tests the performance of arithmetic operations (-
, %
, *
, +
, and **
) on large numbers represented as either Number
or BigInt
.
Options compared
There are two options being compared:
Pros and cons of each approach:
Library used
The benchmark uses the BigInt
library, which is a polyfill that provides support for BigInt
in older browsers that don't natively support it. This allows the benchmark to run on a wider range of environments.
Special JS feature or syntax
This benchmark doesn't explicitly use any special JavaScript features or syntax beyond what's required for testing the Number
and BigInt
data types. However, it's worth noting that modern JavaScript engines are optimized for performance and may take advantage of certain browser-specific features or optimizations.
Other alternatives
If you were to write a similar benchmark from scratch, you might consider using:
Keep in mind that the specific implementation details may vary depending on your goals, requirements, and target audience.
Benchmark preparation code
The provided Script Preparation Code
initializes variables with large numbers for both Number
and BigInt
, ensuring consistent test cases:
var bigInt1 = 1000000n;
var bigInt2 = 5000000n;
var number1 = 1000000;
var number2 = 5000000;
Individual test cases
The benchmark defines two individual test cases, each with a different approach:
Test case 1: BigInt
a = bigInt1
b = bigInt2
c = a - b
c = a % b
c = a * b
c = a + b
c = a ** b
Test case 2: Number
a = number1
b = number2
c = a - b
c = a % b
c = a * b
c = a + b
c = a ** b
Both test cases perform the same arithmetic operations, but using Number
and BigInt
, respectively.