Skip to main content

What is a "bottleneck" in algorithms?

1. What "bottleneck" means

A bottleneck is the part of an algorithm (or a system) that limits overall performance, that is, the slowest or most resource-heavy part, because of which everything else runs slower.

The name comes from a bottle's neck: liquid flows slowly through a narrow neck, even though the bottle itself can be large.


2. In the context of algorithms

In an algorithm, a bottleneck is an operation or piece of code whose time or space complexity dominates the other parts.

Example:

javascript
function processData(data) { // 1. Fast filtering (O(n)) const filtered = data.filter(x => x > 10); // 2. Sorting (O(n log n)) const sorted = filtered.sort((a, b) => a - b); // 3. A light pass (O(n)) return sorted.map(x => x * 2); }

The bottleneck here is sorting (O(n log n)), because it is what determines the overall performance. Even if you optimize the other steps, the speedup will be minimal.

Overall complexity:

O(n) + O(n log n) + O(n) ≈ O(n log n)


3. An example with a numeric effect

StageTimeShare of the total
Filtering10 ms5%
Sorting170 ms85%
Post-processing20 ms10%

The "bottleneck" is sorting. Optimizing the other 15% will barely give any gain.

Amdahl's law:

improving 90% of the code is pointless if 10% remains the bottleneck.


4. How to find a bottleneck

In the browser:

  • Chrome DevTools → Performance → look for where the CPU is "burning" the most.
  • Lighthouse → shows "Long tasks", "Recalculate Style", "Layout".

In Node.js:

  • --inspect → a flamegraph in DevTools;
  • clinic.js, 0x, node --prof;
  • measurements with performance.now(), console.time().

In algorithms:

  • Theoretical complexity analysis (Big O);
  • Timing on large inputs;
  • Comparing parts of a function (loop, recursion, sort, filter).

5. Typical bottlenecks in JS code

CategoryExampleWhy it's a "bottleneck"
CPU-boundlarge loops, sorts, recursionblock the event loop
Algorithmsan inefficient data structure (searching an array instead of a Map)complexity grows
Memorystoring large structures without cleanupGC, lag
I/Onetwork requests, file readswaiting for a response
DOMfrequent reflow/repaintslow rendering
Reactextra re-renders, recreating functionsload on reconciliation

6. How bottlenecks are removed

ApproachWhat it does
Profilingfirst measure where the "bottleneck" is
Algorithm optimizationreplace O(n²) with O(n log n)
Memoization / cachingreuse computations
Parallelization (Web Workers)move CPU-heavy work to another thread
Asynchrony / batchingmake I/O operations non-blocking
Memory optimizationremove unnecessary objects
Choosing a different data structureSet instead of Array.includes(), Map instead of Object

7. An analogy

Imagine a factory:

  • One machine produces 100 parts/min.
  • A second one processes 10 parts/min. → that one becomes the bottleneck.
  • Even if you speed up the first machine, the whole system's speed will not increase until you optimize the second one.

8. Short summary

TermMeaning
BottleneckThe slowest part of an algorithm, limiting overall performance
How it shows upLong execution, high CPU load, delays
How to find itProfiling, measurements, Big O analysis
How to remove itChange the algorithm, the data structure, parallelize

Short Answer

Interview ready
Premium

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