# Why is hashing Dicts faster than Arrays?

**URL:** https://discourse.julialang.org/t/why-is-hashing-dicts-faster-than-arrays/54963
**Category:** Performance
**Created:** [February 10, 2021, 2:49am UTC](https://discourse.julialang.org/t/why-is-hashing-dicts-faster-than-arrays/54963 "2021-02-10T02:49:31Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![bsuwal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bsuwal/32/19577_2.png) [@bsuwal](https://discourse.julialang.org/u/bsuwal)
#### Post date: [February 10, 2021, 2:49am UTC](https://discourse.julialang.org/t/why-is-hashing-dicts-faster-than-arrays/54963/1 "2021-02-10T02:49:31Z")

</div>

I am suprised as to why hashing dicts is faster than arrays:

```julia
d = Dict{Int, Int}([i => i for i in 1:10])
n = 10000000
@btime begin
    for i = 1:n
        hash(d)
    end
end

```

gave me `1.457 s (39998979 allocations: 762.92 MiB) `

while running this

```julia
arr = Vector{Int}([i for i in 1:10])
n = 10000000
@btime begin
    for i = 1:n
        hash(arr)
    end
end

```

gave me `2.325 s (39998979 allocations: 762.92 MiB) `

This is particularly confusing to me because the inner representation of Dict allocates 3 Arrays: `slots`, `keys` and `vals`. If anything, I would have thought that Dicts should be way slower than Arrays, but even the number of allocations are the same?

Relatedly: What is being allocated?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [February 10, 2021, 3:15am UTC](https://discourse.julialang.org/t/why-is-hashing-dicts-faster-than-arrays/54963/2 "2021-02-10T03:15:17Z")

</div>

For longer `Array`, you should see the `Array` win out. What happens is that since we want all `AbstractArray`s that are equal to hash the same, we use a complicated method of hashing approximately `log(length(arr))` values that is probably adding a bunch of overhead. It would probably be a good idea to have a fastpath for small `Array`s that just hashes everything.
