Math.floor(val) vs val >> 0 vs val | 0 (v4)

Revision 4 of this benchmark created on


Description

Figure out what way of integer-ing a value is most efficient

Setup

const values = new Array(10000).fill(null).map(() => Math.random * 100);
let fixed = []

Test runner

Ready to run.

Testing in
TestOps/sec
Math.floor(val)
fixed = [];
for (const val of values) {
	fixed.push(Math.floor(val));
}
ready
val >> 0
fixed = [];
for (const val of values) {
	fixed.push(val >> 0);
}
ready
val | 0
fixed = [];
for (const val of values) {
	fixed.push(val | 0);
}
ready
~~val
fixed = [];
for(const val of values) {
	fixed.push(~~val)
}
ready

Revisions

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