BigInt vs. number operations (v3)

Revision 3 of this benchmark created on


Setup

let sp = 100000;

const numbers = [];

for (let i = 0; i < sp; i++) {
	numbers.push((Math.random() * 0xffffffff) & 0xffffffff);
}

const ints = numbers.map((x) => BigInt(x));

const operations = [
	(a, b) => a + b,
	(a, b) => a - b,
	(a, b) => a * b,
	(a, b) => b == 0 ? b : a / b,
	(a, b) => b == 0 ? b : a % b,
];

const program = Array.from({ length: numbers.length - 1 }, () => (Math.random() * operations.length) | 0);

function popNumber() {
	return numbers[--sp];
}

function pushNumber(number) {
	numbers[++sp] = number;
}

function popInt() {
	return ints[--sp];
}

function pushInt(int) {
	ints[++sp] = int;
}

Test runner

Ready to run.

Testing in
TestOps/sec
number
sp = numbers.length;

for (const op of program) {
	pushNumber(operations[op](popNumber(), popNumber()) & 0xffffffff);
}
ready
BigInt
sp = ints.length;

for (const op of program) {
	pushInt(BigInt(Number(operations[op](popInt(), popInt())) & 0xffffffff));
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.