Space Complexity
Space complexity is how extra memory an algorithm needs as the input grows. It counts the structures you allocate, not the input the caller already owns.
Detailed Explanation
Time asks how much work. Space asks how much RAM. The two trade: you can spend a Set to turn an O(n^2) scan into O(n) time, and that Set is O(n) extra space.
Auxiliary space is the usual interview number: memory besides the input and the output. Reversing an array in place is O(1) auxiliary. Building a reversed copy is O(n). If the problem says "return a new array", the output is required and people still quote auxiliary space for the rest.
Recursion uses the call stack. Each nested call holds its locals until it returns. A recursion of depth n is O(n) space even if you allocated no arrays. Tail-call elimination is not a thing you can rely on in JavaScript; engines do not guarantee it. A loop with one accumulator is O(1) where the recursive twin was O(n) stack.
JavaScript allocations that count: arrays, objects, Map, Set, strings you build (strings are immutable; concatenation in a loop can be O(n^2) time and a pile of temporary strings), closures that keep a large outer array alive, and typed arrays. Hidden allocations: array.slice, spread, array.map, Object.keys, template literals, and JSON.stringify.
A streaming pass that writes one result at a time is O(1) auxiliary if you do not buffer. array.map always buffers n results. for...of plus a push into a filtered array is O(k) for k survivors.
What it costs you: GC and peak memory. Node and browsers die with heap-out-of-memory when a structure grows without bound (a cache with no eviction, [...huge], reading a 2 GB file as a string). Space complexity is how you notice that a "harmless" copy doubled the working set.
Reach for an in-place algorithm when the input is huge and owned by you. Reach for a copy when the input is shared or you need the original. Reach for a bounded cache (Map plus a max size) when the theoretical space is O(n) and n is "all users forever".
Code Examples
O(1) auxiliary: swap in place.
// O(1) auxiliary: swap in place.
function reverseInPlace(arr) {
let i = 0;
let j = arr.length - 1;
while (i < j) {
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i += 1;
j -= 1;
}
return arr;
}
console.log(reverseInPlace(["a", "b", "c"])); // => [ 'c', 'b', 'a' ]O(n) auxiliary: a new array.
// O(n) auxiliary: a new array.
function reversedCopy(arr) {
return [...arr].reverse();
}
console.log(reversedCopy(["a", "b", "c"])); // => [ 'c', 'b', 'a' ]O(n) stack: recursion depth equals n.
// O(n) stack: recursion depth equals n.
function sumRec(arr, i = 0) {
if (i >= arr.length) return 0;
return arr[i] + sumRec(arr, i + 1);
}
function sumIter(arr) {
let total = 0; // O(1) extra
for (const n of arr) total += n;
return total;
}
console.log(sumRec([1, 2, 3]), sumIter([1, 2, 3])); // => 6 6Common Gotchas
Recursion depth is space
a linear recursive walk of a 100k list can throw `RangeError: Maximum call stack size exceeded` before you run out of heap.
Spread and `map` always allocate
`[...arr]` is O(n) extra, even if you throw the copy away a line later.
String `+=` in a loop allocates many intermediates
collect parts in an array and `join("")` once, which is O(n) time and one result string.
A closure can pin more than the algorithm "uses"
an unused large binding in the same environment may still stay alive depending on engine optimisations. Do not close over huge objects you do not need.