JS Arrays: Push/Pop vs Shift/Unshift Performance

Day 17 of me reading random and basic but important coding facts.... Today I read about Arrays in JS and turns out, they aren't just simple lists..... Push/Pop vs. Shift/Unshift Push /Pop is very fast as it just adds/removes from the end.... while Shift/Unshift is very slow.....becoz when we remove the first element, the engine has to physically move every single other element to the left to fix the indices....... and another very very interesting fact is that the length property isn't read-only.............. If we set arr.length = 0, it instantly clears the array. If we manually decrease the length (e.g., arr.length = 2), it truncates the data from the end.....and there is no coming back....... So, use them wisely..... Keep Learning!!!!! #javascript #coding #webdevelopment #learning #frontendDev

To view or add a comment, sign in

Explore content categories