View Source Big-O Time Complexities

Map

OperationTime Complexity
AccessO(log n)
SearchO(log n)
InsertionO(log n)
DeletionO(log n)
SizeO(1)

Caveats:

  • Maps are unordered, allow any key type, but disallow duplicate keys

MapSet

Same complexities as 'Map'

List

OperationTime Complexity
AccessO(n)
SearchO(n)
InsertionO(1) for prepending, otherwise O(n)
DeletionO(1) if first element, otherwise O(n)
LengthO(n)

Caveats

  • Lists are not arrays as in other languages, but single-linked lists

Keyword (List)

A Keyword (list) has the same time complexities as List. Every entry in a Keyword (list) is a tuple with the first element being the key and the second element the value.

keyword = [{:a, 1}, {:b, 2}]
# Can also be written as:
keyword = [a: 1, b: 2]

Caveats

  • Keys must be atoms.
  • Keys are ordered, as specified by the developer.
  • A keyword list may have duplicate keys, but duplicates are often ignored by functions like Keyword.get/3 (returns the first match) and are even removed by e.g. Keyword.put/3 and Keyword.delete/2.
    iex> Keyword.get([{:a, 1}, {:a, 2}], :a)
    1
    
    iex> Keyword.put([{:a, 1}, {:a, 2}], :a, 3)
    [a: 3]
    
    iex> Keyword.delete([{:a, 1}, {:a, 2}], :a)
    []

Tuple

OperationTime Complexity
AccessO(1)
SearchO(n)
InsertionO(n)
DeletionO(n)
LengthO(1)

Caveats

(erlang) array

OperationTime Complexity
AccessO(log n) [7]
SearchO(log n)
InsertionO(log n)
DeletionO(log n)
LengthO(log n)

Caveats

  • An array is a trie of small tuples, with a smaller memory overhead to Lists
  • Deletion is actually a replace with the default value and creates sparse arrays

:queue

OperationTime Complexity
AccessO(1) for first and last element, O(n) otherwise
SearchO(n)
InsertionO(1)
DeletionO(n)
LengthO(n)

:ets

OperationTime Complexity
Access/Search/InsertionO(1) for set, O(log n) for ordered_set where n is the table size, O(n) for bag and duplicate_bag where n is the number of duplicate keys
DeletionO(1) for set, O(n) for ordered_set (ref), ? for bag and ordered_bag
Length?, depends on the decentralized_counters table option (ref)

:persistent_term

OperationTime Complexity
AccessO(1)
SearchO(1)
InsertionO(n) where n is the number of references to the term
DeletionJust don't (ref)
Length?

References