Skip to content

Instantly share code, notes, and snippets.

@alyssaq
Last active December 22, 2015 01:29
Show Gist options
  • Save alyssaq/6396729 to your computer and use it in GitHub Desktop.
Save alyssaq/6396729 to your computer and use it in GitHub Desktop.
Javascript-BigO

Arrays in javascript are simply objects.

Access - O(1) Beware in IE
Appending (push) - Amortized O(1) (sometimes resizing the hashtable is required;)
Prepending (unshift) - O(n), since it requires reassigning all the indexes
Insertion - Amortized O(1) if the value does not exist. O(n) if you want to shift existing values (Eg, using splice).
Deletion at end (pop) - O(1)
Deletion anywhere else (slice) - O(n) since indices need to be reassigned.
Swapping - O(1)

array.splice(index to add/remove, howmany, item1,.....,itemX)
array.slice(start, end): returns new array. Negative index to start from end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment