You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While looking through the sourcecode I noticed that the shift and pop functions actually set the removed element to undefined, while all other functions don't.
It is the most obvious with the clear function where it just sets this._head = this._tail = 0;. Sure it states that the capacity remains unchanged, but that is not the problem. As long as the elements are not set to undefined, the array holds references to the elements in memory, preventing the garbage collector from freeing said memory.
As an example (ignoring memory other then the data objects):
Push 10x 100MB Objects into the queue (for example large strings, or other data buffers, or at a smaller scale very large JSON objects)
Now the process will use 1000MB of memory
Shift 10x
Now the process will use 0MB (the array stores only references, so an array with 10x undefined doesn't take use much memory itsef)
Push 10x 100MB Objects again
Process will use 1000MB of memory again
Call clear
Process still uses 1000MB of memory, because the array still holds the references
In both cases the capacity of the internal array will remain unchanged, but with clear it creates kind of a leak.
I don't think either behavior is wrong, but the undocumented inconsistency is not good imo. If it is ok to leak the deleted cells, the same behavior could be used in shift / pop and likely improve performance in those functions. If it is considered to be not ok in shift / pop, it shouldn't be ok for the other functions either.
Edit: The builtin javascript Array functions splice and length=x do in fact seem to free the references of all removed elements
While looking through the sourcecode I noticed that the
shiftandpopfunctions actually set the removed element toundefined, while all other functions don't.It is the most obvious with the
clearfunction where it just setsthis._head = this._tail = 0;. Sure it states that the capacity remains unchanged, but that is not the problem. As long as the elements are not set toundefined, the array holds references to the elements in memory, preventing the garbage collector from freeing said memory.As an example (ignoring memory other then the data objects):
In both cases the capacity of the internal array will remain unchanged, but with
clearit creates kind of a leak.I don't think either behavior is wrong, but the undocumented inconsistency is not good imo. If it is ok to leak the deleted cells, the same behavior could be used in
shift/popand likely improve performance in those functions. If it is considered to be not ok inshift/pop, it shouldn't be ok for the other functions either.Edit: The builtin javascript Array functions
spliceandlength=xdo in fact seem to free the references of all removed elements