var bigInt1 = 9007199254740992n //MaxSafe +1
var bigInt2 = 103n
var number1 = 9007199254740990 //MaxSafe -1
var number2 = 103
var pgMaxBigInt = 9223372036854775806n // 2^63-1
var pgDiv = 103n
var pgDecimal = 92233720368547758070001230n // max digits before decimal point: 131072
var i128 = 17014118346046923173168730371588410572712345n //big
var i128Div = 103n
var i256 = 1701411834604692317316873037158841057271234517014118346046923173168730371588410572712345n //rlybig
var i256Div = 103n
a = bigInt1
b = bigInt2
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
a = pgMaxBigInt
b = pgDiv
c = a - b
c = a % b
c = a * b
c = a + b
a = i128
b = i128Div
c = a - b
c = a % b
c = a * b
c = a + b
a = i128
b = i128Div
c = a - b
c = a % b
c = a * b
c = a + b
a = i256
b = i256Div
c = a - b
c = a % b
c = a * b
c = a + b
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
2 | |
3 | |
4 | |
5 | |
i256 |
Test name | Executions per second |
---|---|
1 | 695759.8 Ops/sec |
2 | 679446.9 Ops/sec |
3 | 657925.0 Ops/sec |
4 | 603954.8 Ops/sec |
5 | 606675.0 Ops/sec |
i256 | 580126.2 Ops/sec |
Let's dive into the explanation of what is being tested on MeasureThat.net.
Benchmark Overview
The benchmark compares the performance of different data types in JavaScript for arithmetic operations (addition, subtraction, multiplication, and division) with large numbers. The test cases use various libraries and special features to ensure a comprehensive comparison.
Test Cases
There are six test cases:
Number
and BigInt
data types for arithmetic operations.i128
and i256
) to compare their performance with the BigInt
data type.Libraries Used
BigInt
function is used to create large integer values. It was introduced in ECMAScript 2019.Number
data type is used for regular integers.Special Features and Syntax
BigInt
data type is used to create large integer values. Its syntax is n
(e.g., 9007199254740992n
). Note that this syntax was only introduced in ECMAScript 2019, so older browsers may not support it.i128
and i256
variables are used to represent large integers. These values are not standard JavaScript literals.Comparison Options
The benchmark compares the performance of the following options:
These comparisons help determine which data type performs better for large arithmetic operations.
Pros and Cons of Different Approaches
BigInt
.Other Considerations
Overall, this benchmark helps developers understand the performance differences between various data types in JavaScript for large arithmetic operations, making informed decisions about which data type to use depending on the specific requirements of their project.