perf($parse): calculate array lengths once at start of loop

Part of #8901