Note that growing the array doesn't have to move all of the contents of the array, just the memory for the array itself (mostly the references to the elements of the array).
Shrinking an array will never move its memory block. I doubt it will free any memory either, though undefing it will allow that memory to be reused.
In theory, pop()ing would be more efficient than shift()ing since every so often the array will be moved down so that the gap in front of its memory space is not huge. But in practice this is done infrequently enough that it usually doesn't matter. However, if you have a huge array, I'd probably play it safe and use push() and pop() and pre-extend the space for the array if possible.
See also Shift, Pop, Unshift and Push with Impunity!.
This node has been updated.
- tye (but my friends call me "Tye")In reply to (tye)Re: Memory efficiency with arrays
by tye
in thread Memory efficiency with arrays
by Hot Pastrami
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |