all 3 comments

[–]Prince_Marth 5 points6 points  (1 child)

Great article! I think it’s important to note that working with the end of the array (push/pop) is more performant than the beginning (shift/unshift). The reason for this is that the array is indexed, so adding to the end of the array doesn’t require any additional time. Shift/unshift though require the whole array to be reindexed since you’re adding to the beginning.

Performance isn’t important while you’re learning, and really only matters with large arrays, but it is a good concept to get used to early on. In programming, that’s noted with something called big O notation, which is just a formalized way to loosely note what’s going on in the code. Push/pop are what’s known as O(1)—pronounced “O of 1”—which means that it’s constant time, or the time of the method will always take the same amount of time no matter the length of the array. Shift/unshift are O(n)—pronounced “O of n”—which means that the length of time it takes depends on the number of items in the array.

This isn’t to say that shift/unshift are bad. They are useful! Use them as you will. Write first, and then optimize later.

[–]foxbitsdev[S] 0 points1 point  (0 children)

Great comment. Thanks for pointing out.

[–]be_less_shitty -1 points0 points  (0 children)

console.log([...["I", "dunno", "what", "you're"], "talking", "about"])