Even Odd (Array || TypedArray) split

Benchmark created on


Description

Outputting two new Arrays or TypedArrays that are even and odd split of an input Array.

Setup

const size = 2**12
let input = new Float32Array(size, () => Math.random())

Test runner

Ready to run.

Testing in
TestOps/sec
Filter
() => {
	return [
	input.filter((_, i) => i & 1),
	input.filter((_, i) => !(i & 1))
	]
}
ready
for loop
() => {
	const l = input.length
	const l2 = Math.ceil(l / 2)
	const a = new input.constructor(l2)
	const b = new input.constructor(l2)
	let j = 0, k = 0
	for(let i = 0; i < l; i++) {
		if(i & 1) b[j++] = input[i]
		else a[k++] = input[i]
	}
	return [a, b]
}
ready
Reduce
() => {
  const l = input.length;
  const l2 = Math.ceil(l / 2);
  const [a, b] = input.reduce(
    ([even, odd], value, index) => {
      if (index & 1) odd[index >> 1] = value;
      else even[index >> 1] = value;
      return [even, odd];
    },
    [new input.constructor(l2), new input.constructor(l2)]
  );
  return [a, b];
}
ready
Separate for loops
() => {
  const l = input.length;
  const l2 = Math.ceil(l / 2);
  const a = new input.constructor(l2);
  const b = new input.constructor(l2);
  let j = 0;
  for (let i = 0; i < l; i += 2) {
    a[j++] = input[i];
  }
  j = 0;
  for (let i = 1; i < l; i += 2) {
    b[j++] = input[i];
  }
  return [a, b];
}
ready
.from() mapFn()
() => {
  const l2 = Math.ceil(input.length / 2);
return [input.constructor.from({ length: l2 }, (_, i) => input[i * 2]), input.constructor.from({ length: l2 }, (_, i) => input[i * 2 + 1])];
}
ready

Revisions

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