DEV Community

Tech Madi
Tech Madi

Posted on

#webDev #VanillaJS

Performance

LuxTech Academy had a 100daysofcode challenge .I am half of the days and I learned something quite interesting
Every javascript beginner is always told to master the basics first the exceed to the intermediate level.Something funny is they are not told how to improve .So today we shall discuss on perfomance on arrays.

Should work with the front of an array or the back of an array?

Working on the end of an array would boost your performances.push() & pop() work fast compared to shift() & unshift()

When working with shift() the following happens:-

  • The element with the index 0 is removed
  • All elements to be moved to the left
  • Update the length property

The more elements in the array, the more items to move , the more memory operation to perform

As for push() & pop() no moving of array items is involved.pop() cleans the index and shortens the length
Hence working with the end of an array is more effective

Top comments (0)