I. Objects

In Javascript, you use Object when:

  • you don;t need order
  • Need faster access, insertion, and removal

The object itself is really fast, let’s see its big O:

  • Insertion: O(1)
  • Removal: O(1)
  • Searching O(n)
  • Access: O(1)

II. Array

In contrast to Object, Arrays are ordered lists, they have indexes. Use array when you need:

  • You need order
  • Need fast access, insertion, removal

Big O of Array is a bit more complicated in comparison with Object:

  • Searching: O(n)
  • Access: O(1)
  • Insertion: depends …
  • Removal: depends …

If you try to insert an element to the end of an array, it’s gonna be easy. But inserting an element into the beginning of an array, the index is totally changed for another element, and it’s costly. The same behavior is applied to the removal.

Big O of array Operation:

  • push: O(1)
  • pop: O(1)
  • shift: O(n)
  • unshift: O(n)
  • concat: O(n)
  • slice: O(n)
  • splice: O(n)
  • sort: O(n*log n)
  • forEach/map/filter/reduce…: O(n)

Leave a Reply

Your email address will not be published. Required fields are marked *