Array Performance
Array Performance
Section titled “Array Performance”Introduction
Section titled “Introduction”Different array operations have different time complexities. Understanding these helps you write efficient code.
Performance Table
Section titled “Performance Table”| Operation | Time Complexity | Method |
|---|---|---|
| Access by index | O(1) | arr[i] |
| Add to end | O(1) | push() |
| Remove from end | O(1) | pop() |
| Add to start | O(n) | unshift() |
| Remove from start | O(n) | shift() |
| Insert/Remove | O(n) | splice() |
| Search (unsorted) | O(n) | find(), indexOf() |
| Sort | O(n log n) | sort() |
| Iteration | O(n) | forEach(), map() |
Visual Explanation
Section titled “Visual Explanation”flowchart LR A["push/pop: O(1)"] B["shift/unshift: O(n)"] C["Access: O(1)"] D["Search: O(n)"]
subgraph Fast A C end
subgraph Slower B D endBest Practices
Section titled “Best Practices”- Prefer
push/popovershift/unshiftfor queue-like operations - Use typed arrays for large numeric datasets
- Avoid frequent
spliceon large arrays
Summary
Section titled “Summary”- End operations (push/pop) are O(1) — fast
- Start operations (shift/unshift) are O(n) — slower
- Index access is O(1) — instant