Array.Shift speed

I got to thinking tonight that before blindly assuming that unshift in JavaScript was slow, but shift was fast, that I should double check that.

As it turns out, shift is roughly as slow as unshift on arrays, which means I need to re-think how I would implement a queue in JavasScript. Just using array.push() and array.shift() works, but is unnecessarily slow.

My initial thought is to use a linked list that has both a head and a tail pointer. I’ll need to test out the speed of that kind of construct against the speed of array.push() and array.shift(). It’s possible that there are some other factors that would slow a linked list down in comparison for use as a queue.

More on that once I’m done testing.

Leave a Reply