Skip to main content

Performance & Complexity

This document provides a comprehensive Big-O reference for time and space complexity across all Dart collection types.


1. Master Time Complexity Table

Collection TypeAccess [i]Lookup (contains)Insert (Front)Insert (Back)Insert (Middle)Remove (Front)Remove (Back)Remove (Middle)
List (Growable)O(1)O(n)O(n)O(1)*O(n)O(n)O(1)O(n)
List (Fixed)O(1)O(n)N/AN/AN/AN/AN/AN/A
Set (LinkedHashSet)N/AO(1)*N/AO(1)*N/AN/AO(1)*O(1)*
HashSetN/AO(1)*N/AO(1)*N/AN/AO(1)*O(1)*
SplayTreeSetN/AO(log n)*N/AO(log n)*N/AN/AO(log n)*O(log n)*
Map (LinkedHashMap)N/AO(1)* (by key)N/AO(1)*N/AN/AO(1)*O(1)*
HashMapN/AO(1)* (by key)N/AO(1)*N/AN/AO(1)*O(1)*
SplayTreeMapN/AO(log n)* (key)N/AO(log n)*N/AN/AO(log n)*O(log n)*
Queue (ListQueue)N/AO(n)O(1)*O(1)*N/AO(1)O(1)O(n)
DoubleLinkedQueueN/AO(n)O(1)O(1)O(1)**O(1)O(1)O(1)**
LinkedListN/AO(n)O(1)O(1)O(1)**O(1)O(1)O(1)**

* Amortized runtime.
** Requires existing node/entry reference.


2. Space Complexity & Overhead

Collection TypeSpace Overhead per ElementMemory LayoutNotes
ListMinimal (Contiguous Array)Flat memory blockMay allocate 1.5x–2x capacity buffer for growth
HashSetMediumHash Table + BucketsTable size dynamically scales
LinkedHashSetMedium-HighHash Table + Doubly-linked PointersMaintains insertion order pointers
SplayTreeSetHighBinary Search Tree NodesLeft, Right, Parent pointers per node
Queue (ListQueue)LowRing Buffer ArrayContiguous memory with head/tail pointers
LinkedListHighDoubly-linked Node ObjectsRequires element class to extend LinkedListEntry

3. Performance Gotchas in Dart

Gotcha 1: List.removeAt(0) is O(n)

Removing from index 0 shifts every remaining item left. Use Queue if doing frequent front removals.

Gotcha 2: Lazy Iterable Re-evaluations

Iterating over a lazy where() or map() repeatedly evaluates the transform function every single time.

final lazyIterable = hugeList.where((x) => expensiveTest(x));

// Runs expensiveTest twice for every item!
print(lazyIterable.length);
print(lazyIterable.first);

// Fix: Materialize to a concrete List first
final concreteList = lazyIterable.toList();

Gotcha 3: Map / Set Hash Collisions

If hashCode is poorly implemented (e.g., returning constant 1), operations degrade from O(1) to O(n).