↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Time complexity Big O for Javascript Array methods and examples

The post groups common JavaScript array methods by what they do and gives each one’s Big O time complexity. It covers mutator, accessor, and iteration methods, then adds some() and every() as bonus cases.

Reading notes
#

  • push() and pop() are O(1) because they add or remove an element at the end of the array.
  • shift(), unshift(), and splice() are O(n) because they can move elements in the array.
  • sort() is O(n log(n)) and can use a compare function for custom ordering.
  • concat(), slice(), and indexOf() are O(n).
  • forEach(), map(), filter(), and reduce() are O(n) because they process each element in the array.
  • some() returns true when it finds one or more matching items, and false otherwise.
  • every() returns true only when all items match the given condition.
  • The conclusion says understanding these methods helps calculate the time complexity of larger algorithms built from arrays.