# Could someone demystify arrays for me?

**URL:** https://discourse.julialang.org/t/could-someone-demystify-arrays-for-me/38643
**Category:** Performance
**Created:** [May 3, 2020, 3:22am UTC](https://discourse.julialang.org/t/could-someone-demystify-arrays-for-me/38643 "2020-05-03T03:22:34Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![krstoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krstoff/32/14448_2.png) [@krstoff](https://discourse.julialang.org/u/krstoff)
#### Post date: [May 3, 2020, 3:22am UTC](https://discourse.julialang.org/t/could-someone-demystify-arrays-for-me/38643/1 "2020-05-03T03:22:34Z")

</div>

I was having trouble finding performance characteristics for `push!` and `pushfirst!` on `Vector{T}` so I wrote a quick test comparing the two and was surprised to find that they were the same speed - even when intermixed. I did my best to hunt around for the answer and got as far as [this function in array.c](https://github.com/JuliaLang/julia/blob/7426625b5c07b0d93110293246089a259a0a677d/src/array.c#L735) until I decided I would give up and just ask.

When I allocate an empty array of size n, does it start adding to the middle instead of the front of the memory allocation? Doesn’t that have (admittedly small) performance implications for the usual case of just pushing things onto the end? Is there a way to change this behavior?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 3, 2020, 5:36am UTC](https://discourse.julialang.org/t/could-someone-demystify-arrays-for-me/38643/2 "2020-05-03T05:36:48Z")

</div>

Vectors (implemented `jl_array_t`) can have extra unused space _before_ the elements (see the `offset` field in the C code). So `pushfirst!` will allocate extra space and only move/reallocate occasionally.

This is not exactly overdocumented, but it is [mentioned in passing in the devdocs](https://docs.julialang.org/en/v1.5-dev/devdocs/isbitsunionarrays/#isbits-Union-Arrays-1).
