Math.floor vs Math.round vs parseInt vs Bitwise (v162)

Revision 162 of this benchmark created on


Description

Rounding in all form possible

Setup

const numA = Math.random() * 1000000000;
const numB = Math.random() * 1000000000;
const numC = Math.random() * 1000000000;

const a = numA / numB;
const b = numA / numC;
const c = numB / numA;
const d = numB / numC;
const e = numC / numA;
const f = numC / numB;

function test(fn) {
	fn(a);
	fn(b);
	fn(c);
	fn(d);
	fn(e);
	fn(f);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Math.floor
test((x) => Math.floor(x));
ready
Math.round
test((x) => Math.round(x));
ready
parseInt
test(parseInt);
ready
Bitwise |
test((x) => x | 0);
ready
Bitwise >>
test((x) => x >> 0);
ready
Bitwise ~~
test((x) => ~~x);
ready
Bitwise <<
test((x) => x << 0);
ready
Cached Math.floor
test(Math.floor);
ready
Cached Math.round
test(Math.round);
ready
Hack with bitwise shift
test((x) => (0.5 + x) << 0);
ready
toFixed
test((x) => x.toFixed(0));
ready
Hack ~~
test((x) => ~~(0.5 + x));
ready
Hack Bitwise |
test((x) => (0.5 + x) | 0);
ready

Revisions

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