Space Complexity

1 min read
Rapid overview

Space Complexity

Space complexity measures how much memory an algorithm uses relative to input size.

// O(1) extra space
const max = (items: number[]) => {
  let value = -Infinity;
  for (const item of items) value = Math.max(value, item);
  return value;
};

// O(n) extra space
const copy = (items: number[]) => [...items];

Notes

  • Prefer in-place operations when safe.
  • Avoid accidental allocations in hot loops.