Suggest an editImprove this articleRefine the answer for “What is a "bottleneck" in algorithms?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)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. **Key point:** the name comes from a bottle's neck - liquid flows slowly through a narrow neck, even though the bottle itself can be large.Shown above the full answer for quick recall.Answer (EN)Image## 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 | Stage | Time | Share of the total | |---|---|---| | Filtering | 10 ms | 5% | | Sorting | 170 ms | **85%** | | Post-processing | 20 ms | 10% | 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 | Category | Example | Why it's a "bottleneck" | |---|---|---| | CPU-bound | large loops, sorts, recursion | block the event loop | | Algorithms | an inefficient data structure (searching an array instead of a Map) | complexity grows | | Memory | storing large structures without cleanup | GC, lag | | I/O | network requests, file reads | waiting for a response | | DOM | frequent reflow/repaint | slow rendering | | React | extra re-renders, recreating functions | load on reconciliation | --- ## 6. How bottlenecks are removed | Approach | What it does | |---|---| | **Profiling** | first measure where the "bottleneck" is | | **Algorithm optimization** | replace O(n²) with O(n log n) | | **Memoization / caching** | reuse computations | | **Parallelization (Web Workers)** | move CPU-heavy work to another thread | | **Asynchrony / batching** | make I/O operations non-blocking | | **Memory optimization** | remove unnecessary objects | | **Choosing a different data structure** | `Set` 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 | Term | Meaning | |---|---| | **Bottleneck** | The slowest part of an algorithm, limiting overall performance | | **How it shows up** | Long execution, high CPU load, delays | | **How to find it** | Profiling, measurements, Big O analysis | | **How to remove it** | Change the algorithm, the data structure, parallelize |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.