var bigInt32 = 2n ** 24n;
var bigInt64 = 2n ** 40n;
var bigInt128 = 2n ** 80n;
var bigIntNum = 2n;
var int32 = 2 ** 10;
var int64 = 2 ** 40;
var float = Math.fround(2 ** 20);
var double = 2 ** 80;
var intNum = 2;
const imul = Math.imul
const fround = Math.fround
let a = bigInt32;
let b = bigIntNum;
let c_sub, c_mod, c_mul, c_add;
c_sub = a - b;
c_mod = a % b;
c_mul = a * b;
c_add = a + b;
let a = bigInt64;
let b = bigIntNum;
let c_sub, c_mod, c_mul, c_add;
c_sub = a - b;
c_mod = a % b;
c_mul = a * b;
c_add = a + b;
let a = bigInt128;
let b = bigIntNum;
let c_sub, c_mod, c_mul, c_add;
c_sub = a - b;
c_mod = a % b;
c_mul = a * b;
c_add = a + b;
let a = int32;
let b = intNum;
let c_sub, c_mod, c_mul, c_add;
c_sub = a - b;
c_mod = a % b;
c_mul = a * b;
c_add = a + b;
"use asm";
let a = int32 | 0;
let b = intNum | 0;
let c_sub, c_mod, c_mul, c_add;
c_sub = (a - b) | 0;
c_mod = (a % b) | 0;
c_mul = imul(a, b);
c_add = (a + b) | 0;
let a = int64;
let b = intNum;
let c_sub, c_mod, c_mul, c_add;
c_sub = a - b;
c_mod = a % b;
c_mul = a * b;
c_add = a + b;
"use asm";
let a = float;
let b = fround(intNum);
let c_sub, c_mod, c_mul, c_add;
c_sub = fround(a - b);
c_mod = fround(a % b);
c_mul = fround(a * b);
c_add = fround(a + b);
let a = double;
let b = intNum;
let c_sub, c_mod, c_mul, c_add;
c_sub = a - b;
c_mod = a % b;
c_mul = a * b;
c_add = a + b;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
BigInt (32) | |
BigInt (64) | |
BigInt (128) | |
Number (Int32) | |
Number (Int32 asm.js) | |
Number (maybe Int64) | |
Number (Float asm.js) | |
Number (Double) |
Test name | Executions per second |
---|---|
BigInt (32) | 169096288.0 Ops/sec |
BigInt (64) | 177226400.0 Ops/sec |
BigInt (128) | 183639712.0 Ops/sec |
Number (Int32) | 185756720.0 Ops/sec |
Number (Int32 asm.js) | 172968016.0 Ops/sec |
Number (maybe Int64) | 181406160.0 Ops/sec |
Number (Float asm.js) | 193061312.0 Ops/sec |
Number (Double) | 181440800.0 Ops/sec |
The benchmark represented in the provided JSON tests the performance of various numerical operations using different types of number representations in JavaScript. It particularly focuses on two categories: BigInt and Number, comparing their performance across different bit-widths and methodologies. Let’s break down the test cases, options compared, as well as their pros and cons:
BigInt Tests:
BigInt (32)
: Tests operations using a 32-bit BigInt.BigInt (64)
: Tests operations using a 64-bit BigInt.BigInt (128)
: Tests operations using a 128-bit BigInt.These cases perform basic arithmetic operations: subtraction, modulus, multiplication, and addition between big number types and standard integers.
Number Tests:
Number (Int32)
: Tests operations using a 32-bit integer (which is a regular JavaScript number).Number (Int32 asm.js)
: Uses asm.js to optimize performance for 32-bit integers explicitly, leveraging bitwise operations.Number (maybe Int64)
: Examines operations potentially using a 64-bit integer. JavaScript’s Number
type can safely represent 53 bits of precision.Number (Float asm.js)
: Similar to Int32 asm.js
, but optimized for floating-point numbers using the Math.fround
method.Number (Double)
: Tests operations using a double-precision floating point.Pros:
2^53 - 1
, which is JavaScript's maximum safe integer for standard numbers.Cons:
Number
types due to extra handling for large integers.Number
; for example, they cannot participate in mathematical functions without converting to regular Number
.Pros:
Cons:
2^53 - 1
, which can lead to data loss for large integer calculations.Pros:
asm.js
, the performance can be closer to C/C++.Cons:
Number
range."use asm"
directive indicates that the code is intended to be optimized for speed.Other numerical handling options include:
BigInt
.In summary, this benchmark examines the performance of operations using different numerical representations in JavaScript, providing insights into the pros and cons of BigInt vs. Number types, and leveraging special features like asm.js for optimized performance. Ultimately, the choice between these approaches depends largely on the specific application's needs—especially regarding precision and performance.