I believe this comes from the fact that local-variable lookup is faster than global-variable lookup - because the former is lookup-by-index and the latter is lookup-by-name.
The same is true in Julia: “Any code that is performance critical or being benchmarked should be inside a function.” [0]
[0] https://docs.julialang.org/en/v1/manual/performance-tips/ind...