Home

Kenny Porterfield
Analyzing the Performance of Builtin JavaScript Data Structures

Analyzing the Performance of Builtin JavaScript Data Structures

Objects

What is a JS Object?

  • Unordered key-value pairs
  • Work well when you don't need order
  • Work well you when you need fast access/insertion and removal

Big O of Object Operations

  • Insert - O(1)
  • Delete - O(1)
  • Search - O(N)
  • Read - O(1)

Big O of Object Methods

  • Object.keys - O(N)

  • Object.values - O(N)

  • Object.entries - O(N)

  • hasOwnProperty - O(1)

Arrays

What is an array?

  • Ordered list of index-value pairs
  • Good for when you need order

Big O of Array Operations

  • Insert - depends...
    • Inserting at the end of an array is simply 1 step
    • Inserting at the beginning of an array of N length takes N steps because you have to shift/re-index each value that comes after the insertion
  • Delete - depends...
    • Deleting at the end of an array is simply 1 step
    • Deleting at the beginning of an array of N length takes N steps because you have to shift/re-index each value that comes after the deletion
  • Search - O(N)
  • Read - O(1)

Big O of Array Methods

  • push - O(1)
  • pop - O(1)
  • shift - O(N)
  • unshift - O(N)
  • concat - O(N)
  • slice - O(N)
  • splice - O(N)
  • sort - O(N * logN)
  • forEach/map/filter/reduce/etc. - O(N)