Min for int32 numbers

Benchmark created on


Setup

const minMath = (x, y) => Math.min(x, y);
const minTernary = (x, y) => x < y ? x : y;
const minInt32BitOps = (x, y) => x & ((x - y) >> 31) | y & (~(x - y) >> 31);

function getRandomInt32() {
  const min = -2147483648; // minimum value of a 32-bit signed integer
  const max = 2147483647; // maximum value of a 32-bit signed integer
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

let bh = 0;

Teardown

if (Math.random() > 1) console.log(bh);

Test runner

Ready to run.

Testing in
TestOps/sec
Math.min
bh += Math.min(getRandomInt32(), getRandomInt32());
ready
Math.min+fn
bh += minMath(getRandomInt32(), getRandomInt32());
ready
Ternary
bh += minTernary(getRandomInt32(), getRandomInt32());
ready
Bit ops
bh += minInt32BitOps(getRandomInt32(), getRandomInt32());
ready

Revisions

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