Grouping perf

Benchmark created on


Setup

const input = new Array(150).fill().map((_, i) => i + 1);

Test runner

Ready to run.

Testing in
TestOps/sec
Jeremias
const group = (arr, groupLength) => {
  let leadingCount = arr.length % groupLength
  const diff = arr.length - leadingCount
  const elementsPerGroup = diff / groupLength
  const groups = []

  for (let i = 0; i < groupLength; i++) {
    const offsetInitialPosition = Math.min(i, leadingCount)
    let initialPosition = i * elementsPerGroup + offsetInitialPosition

		const offsetFinalPosition = i < leadingCount & 1
    let finalPosition = initialPosition + elementsPerGroup + offsetFinalPosition

    groups.push(arr.slice(initialPosition, finalPosition))
  }

  return groups
}

group(input, 18)
ready
David
const group = (phases, stagesCount) => {
  let groupedPhases = []

  for (let i = stagesCount; i > 0; i--) {
    groupedPhases = [...groupedPhases, phases.splice(0, Math.ceil(phases.length / i))]
  }
  return groupedPhases.flatMap((group, i) => group.map(phase => ({ ...phase, stageIndex: i })))
}

group(input, 18)
ready

Revisions

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