This is a compilation time thing. Creating a new anonymous function has a fixed compilation cost, so map(t -> ..., x)
is slow
julia> x = rand(1:5, 100);
julia> d = Dict(1 => "A", 2 => "B", 3 => "C", 4 => "D", 5 => "E");
julia> @time getindex.(Ref(d), x);
0.144137 seconds (172.92 k allocations: 9.074 MiB, 42.83% gc time)
julia> @time getindex.(Ref(d), x);
0.000016 seconds (4 allocations: 976 bytes)
julia> @time map(xi -> d[xi], x);
0.081783 seconds (98.49 k allocations: 5.256 MiB)
julia> @time map(xi -> d[xi], x);
0.078627 seconds (55.19 k allocations: 2.922 MiB)
julia> get_from_d = let d = d
xi -> d[xi]
end;
julia> @time map(get_from_d, x);
0.043167 seconds (43.72 k allocations: 2.272 MiB)
julia> @time map(get_from_d, x);
0.000024 seconds (2 allocations: 928 bytes)
This re-compilation problem only shows up in global scope, though.
julia> function get_from_d_wrapper(d, x)
map(xi -> d[xi], x)
end;
julia> @time get_from_d_wrapper(d, x);
0.037174 seconds (46.42 k allocations: 2.426 MiB)
julia> @time get_from_d_wrapper(d, x);
0.000006 seconds (1 allocation: 896 bytes)