var bigInt1 = 1000000n
var bigInt2 = 10n
var number1 = 1000000
var number2 = 10
a = bigInt1;
b = bigInt2;
for(let i = 0; i < 1000; ++i) {
c = a << b;
c = a | b;
c = a ^ b;
}
a = number1
b = number2
for(let i = 0; i < 1000; ++i) {
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 | 160787.7 Ops/sec |
Number | 479388.8 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is being tested?
The benchmark compares the performance of using Number
versus BigInt
data types for large numbers in JavaScript. The test case uses a simple loop that performs bitwise operations (<<
, |
, and ^
) on two large numbers.
Options compared:
Pros and Cons:
n
suffix (e.g., 1000000n
) to indicate BigInt literals, which can be unfamiliar to some developers.Other considerations:
BigInt
.BigInt
is expected to perform better than Number
due to its arbitrary-precision arithmetic capabilities.Library usage:
None mentioned in this benchmark definition.
Special JS features or syntax:
None mentioned in this benchmark definition.
Benchmark preparation code explanation:
The script prepares two large numbers (bigInt1
, bigInt2
) and two regular numbers (number1
, number2
). The test loop then performs bitwise operations on these pairs of numbers using the <<
, |
, and ^
operators. The benchmark definition specifies that the test cases use these specific values.
Individual test case explanations:
The two individual test cases are identical, except for the data type used to represent the large numbers:
BigInt
literals with the n
suffix (1000000n
, 10n
). It measures the performance of bitwise operations on these BigInt values.1000000
, 10
) and measures their performance in comparison to the first test case.Latest benchmark result explanation:
The latest benchmark results show that:
Number
option, with an average of 1139.66 executions per second.BigInt
option, but at a slower rate than the Number
option (average of 1208.12 executions per second).Keep in mind that these results might vary depending on the specific hardware and software environment.
Other alternatives:
Some alternative approaches to benchmarking JavaScript performance could include:
BigInt
or Number
with different precision options.However, these alternatives might require additional setup and expertise.