Min variants.

Benchmark created on


Setup

// --- config ---
const N = 100000;           // size of arrays per run
const RANGE = 1e6;          // value range

// --- data (pre-generated once, shared by all tests) ---
const A = new Float64Array(N);
const B = new Float64Array(N);
for (let i = 0; i < N; i++) {
  // Uniform-ish distribution across positive/negative
  A[i] = (Math.random() * 2 - 1) * RANGE;
  B[i] = (Math.random() * 2 - 1) * RANGE;
}

// Global sink to prevent dead-code elimination (shared across tests)
let sink = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
Math.min
let acc = 0;
const len = A.length;
for (let i = 0; i < len; i++) {
  const ai = A[i], bi = B[i];
  acc += Math.min(ai, bi);
}
sink += acc;
ready
let + if
let acc = 0;
const len = A.length;
for (let i = 0; i < len; i++) {
  const ai = A[i], bi = B[i];
  let m = ai;
  if (bi < m) m = bi;
  acc += m;
}
sink += acc;
ready
ternary
let acc = 0;
const len = A.length;
for (let i = 0; i < len; i++) {
  const ai = A[i], bi = B[i];
  const m = ai < bi ? ai : bi;
  acc += m;
}
sink += acc;
ready
branchless
let acc = 0;
const len = A.length;
for (let i = 0; i < len; i++) {
  const ai = A[i], bi = B[i];
  // Branchless: 0.5 * (a + b - |a - b|)
  const m = 0.5 * (ai + bi - Math.abs(ai - bi));
  acc += m;
}
sink += acc;
ready
branchless d-form
let acc = 0;
const len = A.length;
for (let i = 0; i < len; i++) {
  const ai = A[i], bi = B[i];
  const d = ai - bi;
  const m = ai - 0.5 * (Math.abs(d) + d); // min(a,b)
  acc += m;
}
sink += acc;
ready

Revisions

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