There is this myth that assigning array elements is better performant than push
. I believe this originated from Javascript of yore, but is not relevant anymore.
In fact, today push
may be almost equal to or edge out assignment of array elements using keys (depending on runtime engine that is).
A quick test
Let us do a really quick test -
- create an array of 1000 elements, assign each element with key
- compare the performance against just
push
ing a 1000 elements to a blank array
Code below -
|
|
There is a slight edge to push
but nothing that would prevent me from using array keys to assign elements where necessary.
Also see
- [https://jsperf.com/push-method-vs-setting-via-key/3]
- [https://jsperf.com/push-method-vs-setting-via-key/31]