Skip to Content
01 Algorithms and Data Structures02 Arrays and ObjectsArrays and Objects Performance

Arrays and Objects Performance

Understanding the performance characteristics of arrays and objects (hash maps) in TypeScript is crucial for choosing the right data structure for your use case.

Why this matters

Choosing the wrong data structure can turn an O(n) operation into O(n²), causing performance issues at scale. Knowing when arrays are fast vs slow, and when objects are fast vs slow, helps you write efficient code from the start.

Sections

  1. Arrays Performance - Time and space complexity of array operations
  2. Objects Performance - Time and space complexity of object operations

Quick reference

Arrays - Common Operations

OperationTime ComplexityNotes
Access by indexO(1)Fast - direct memory access
Search by valueO(n)Must check each element
Insert at endO(1) amortizedUsually fast, may need resize
Insert at beginningO(n)Must shift all elements
Delete at endO(1)Fast
Delete at beginningO(n)Must shift all elements
SliceO(n)Creates new array

Objects (Hash Maps) - Common Operations

OperationTime ComplexityNotes
Access by keyO(1) averageFast - hash lookup
Search by valueO(n)Must check each value
InsertO(1) averageFast
DeleteO(1) averageFast
Iterate keys/valuesO(n)Must visit all entries
Last updated on