Performance difference between two code

This might be because indexing allocates memory, but it’s hard to tell for sure since you seem to be benchmarking in the global scope which causes problems.