Re:What's wrong with this code, really?
by simonsarris on Friday September 16, 2011 @11:10AM
(#3004734)
Most of the time it is simply because a shift operation has to re-address each element in the array (if it is implemented like a ho-hum, classic array) and a pop operation does not have to do this.
[ Parent
| on HN ]
Re:What's wrong with this code, really?
by sanderjd on Friday September 16, 2011 @01:23PM
(#3005362)
I think the assumption was that even arrays indexed by integers are stored as JS Objects, which are basically a hashmap style structure, and so they wouldn't be implemented like a ho-hum, classic array. I'm not sure if that assumption is right, but if it is, there would be no need to re-address each element.
If you don't re-address each element, how do you map the value at index 1 to index 0?
One way is to leave the indexes intact and keep an offset around, so you may map the nth logical index to the nth physical one. (Add 1 on a shift, subtract 1 on an unshift.)
But if you wanted this behaviour, there are more efficient ways: http://en.wikipedia.org/wiki/Circular_buffer