Skip to content

Array Performance

Different array operations have different time complexities. Understanding these helps you write efficient code.

OperationTime ComplexityMethod
Access by indexO(1)arr[i]
Add to endO(1)push()
Remove from endO(1)pop()
Add to startO(n)unshift()
Remove from startO(n)shift()
Insert/RemoveO(n)splice()
Search (unsorted)O(n)find(), indexOf()
SortO(n log n)sort()
IterationO(n)forEach(), map()
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
end
  • Prefer push/pop over shift/unshift for queue-like operations
  • Use typed arrays for large numeric datasets
  • Avoid frequent splice on large arrays
  • End operations (push/pop) are O(1) — fast
  • Start operations (shift/unshift) are O(n) — slower
  • Index access is O(1) — instant