array method VS for-of

Benchmark created on


Setup

  const possibleMoves = [
    { row: - 2, col: - 1 },
    { row: - 2, col: 1 },
    { row: - 1, col: - 2 },
    { row: - 1, col: 2 },
    { row: 1, col: - 2 },
    { row: 1, col: 2 },
    { row: 2, col: - 1 },
    { row: 2, col: 1 },
  ];

Test runner

Ready to run.

Testing in
TestOps/sec
array method
const lessMovement = possibleMoves
  .map((pos) => ({
    pos,
    count: Math.floor(Math.random() * 10)
  }))
  .sort((a, b) => a.count - b.count)
  .map((item) => item.pos);
ready
for-of
let minLength = Infinity;
let candidates = [];

for (const move of possibleMoves) {
  const nextMovesLength = Math.floor(Math.random() * 10);

  if (nextMovesLength < minLength) {
    minLength = nextMovesLength;
    candidates = [move];
  } else if (nextMovesLength === minLength) {
    candidates.push(move);
  }
}
ready

Revisions

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