Skip to main content

Input size and performance

The larger the input, the longer the code runs, unless the algorithm scales well. The relation between input size and the amount of work is described by asymptotic complexity (Big O), and it is that relation, not the raw speed of a single operation, that decides whether the code survives growth.

Theory

TL;DR

  • Input size affects CPU time, memory use, the number of I/O operations, the number of iterations and the recursion depth.
  • Big O shows how fast the running time grows as n increases, not how many milliseconds one call takes.
  • At n = 10 there is no difference between O(n) and O(n²); at n = 100 000 it decides whether the app works at all.
  • Lookup in a Map or Set is nearly O(1), lookup in an array is O(n), nested loops are O(n²).
  • Long synchronous loops block the event loop: the interface freezes and FPS drops.
  • The main remedies: better data structures, memoization, chunking the work, list virtualization, streaming.

Quick example

javascript
// O(n): time grows in proportion to the number of elements function sum(arr) { return arr.reduce((acc, num) => acc + num, 0); } sum([1, 2, 3]); // roughly 3 operations sum(new Array(1000000)); // roughly 1 000 000 operations // O(n^2): time grows as the square of the number of elements function allPairs(arr) { for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr.length; j++) { // some operation on the pair } } } // n = 1 000 -> about 1 000 000 operations // n = 10 000 -> already 100 000 000 operations

Asymptotic complexity: how time grows

ComplexityNameWhat it meansExample
O(1)constantindependent of the data sizeindex access arr[0]
O(log n)logarithmicgrows very slowlybinary search
O(n)lineartime grows in proportion to the element counta for loop over an array
O(n log n)quasilinearmoderate growthArray.prototype.sort()
O(n²)quadraticexplosive growth on large inputsnested loops
O(2ⁿ)exponentialgrows catastrophicallynaive recursive Fibonacci
O(n!)factorialimpossible for large ngenerating all permutations

The same table from the user's point of view:

Input sizeO(1)O(log n)O(n)O(n²)O(2ⁿ)
10instantinstantinstantinstantinstant
100instantinstantinstantnoticeableslow
1 000instantinstantnoticeableslowimpossible
100 000instantinstantslowimpossibleimpossible

The conclusion: on small inputs anything flies, and inefficiency shows up exactly when the volume has grown.

JavaScript examples

O(1), access does not depend on size:

javascript
const arr = [1, 2, 3, 4, 5]; console.log(arr[3]); // instant, whether there are 5 or 5 million elements

O(n), linear dependence:

javascript
function sum(arr) { return arr.reduce((acc, num) => acc + num, 0); }

The bigger the array, the more time: every element is processed exactly once.

O(n²), quadratic growth:

javascript
function allPairs(arr) { for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr.length; j++) { // some operation } } }

If n = 1000 that is roughly 1 000 000 operations. If n = 10 000, already 100 000 000.

O(2ⁿ), exponential growth:

javascript
function fib(n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); } fib(30); // works fib(45); // already visibly slow

The growth is explosive: at around n = 100 computing it is simply impossible.

Where it shows up in practice

Kind of taskExampleHow input size affects it
Iterating an arraymap, filter, reducelinearly
Searching an arrayarr.includes()linearly
Lookup in an object or Mapobj[key], map.get()nearly O(1)
Sortingarr.sort()O(n log n)
Comparing nested structuresdeep object comparisoncan be O(n²)
Rendering in Reactlarge lists, tablestime grows with the DOM size
Database querieswithout an index it is O(n)the more rows, the slower

Practical consequences of growing data:

  • CPU load rises: operations take longer.
  • Memory use increases, especially when copying or storing large structures.
  • The event loop is blocked: the interface freezes and clicks are not handled.
  • FPS drops: animations become choppy.
  • The garbage collector runs more often, which produces visible lag.
  • Deeper recursion raises the risk of RangeError: Maximum call stack size exceeded.

What to do when the data grows

ProblemSolution
Loops run for too longSplit the work into chunks (setTimeout, requestIdleCallback)
Complex filters and searchesUse Set, Map, prebuilt indexes
Frequent repeated computationMemoization
Too many DOM elementsList virtualization (react-window, infinite scroll)
Constantly recreating arraysUse mutation carefully instead of copying
Large JSON payloadsStreaming reads (ReadableStream)
Heavy computation on the UI threadMove it into a Web Worker

A summary map of the impact:

What growsWhat it affects
Number of iterationsExecution time
Recursion depthRisk of stack overflow
Number of objectsMemory usage
List or array lengthNumber of re-renders in React
I/O volumeLatency from the network or disk

Common mistakes

  1. Optimizing the constant instead of the complexity. Swapping for for while in the name of speed is pointless if the algorithm is still O(n²): the right data structure buys a thousandfold win, a micro-optimization buys percents.
  2. Testing only on small inputs. Anything works on 20 records; problems surface at real volumes, so load checks must use data close to production.
  3. Searching an array inside a loop. arr.includes(x) inside a loop over another array turns the task into O(n * m); a prebuilt Set makes it linear.
  4. Forgetting the hidden cost of built-in methods. arr.splice(), arr.shift() and arr.unshift() shift elements and cost O(n), and a filter().map().reduce() chain walks the array three times.
  5. Assuming O(1) is always faster than O(n). For very small n a plain linear scan can beat building a hash structure: asymptotics describe behaviour under growth, not absolute time.
  6. Blocking the main thread with a synchronous loop. While the loop runs the browser neither paints nor reacts to events, so the user sees a freeze even if the total time is acceptable.
  7. Ignoring memory. An algorithm can be O(n) in time yet allocate O(n) on each step, and growth then hits the garbage collector rather than the CPU.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.