# Is scalar getindex supposed to be allocating

**URL:** https://discourse.julialang.org/t/is-scalar-getindex-supposed-to-be-allocating/135427
**Category:** Performance
**Created:** [February 3, 2026, 2:25pm UTC](https://discourse.julialang.org/t/is-scalar-getindex-supposed-to-be-allocating/135427 "2026-02-03T14:25:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [February 3, 2026, 2:25pm UTC](https://discourse.julialang.org/t/is-scalar-getindex-supposed-to-be-allocating/135427/1 "2026-02-03T14:25:22Z")

</div>

I am a bit stumped by the behavior I have bumped into. I thought that only slicing allocates and scalar indexing should not. But try this thing:

```julia-auto
a = collect(0.0:1.0:10.0)
a[2]

@allocated a[2]

```

For me, it somehow allocates 16 KB. I get the same result on Julia 1.12.4, 1.11.4 and 1.6.7.

Is it really normal?

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [February 3, 2026, 2:29pm UTC](https://discourse.julialang.org/t/is-scalar-getindex-supposed-to-be-allocating/135427/2 "2026-02-03T14:29:23Z")

</div>

It’s an artifact of the REPL

```julia-auto
julia> a = collect(0.0:1.0:10.0)
julia> f(a, i) = a[i]
julia> @allocated f(a,2)
0

```

---

<div class="post-metadata">

### Author: ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)
#### Post date: [February 3, 2026, 2:30pm UTC](https://discourse.julialang.org/t/is-scalar-getindex-supposed-to-be-allocating/135427/3 "2026-02-03T14:30:30Z")

</div>

That’s because `a` is a (non-constant) global variable there.

```julia-auto
julia> const b = collect(0.0:1.0:10.0)
11-element Vector{Float64}:
  0.0
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0
  7.0
  8.0
  9.0
 10.0

julia> @allocated b[2]
0
```
