Repository: withastro/astro
File: packages/astro/src/runtime/server/render/astro/render.ts (TypeScript)

Code before:
				await next.promise;
			}

			// Only create a new promise if rendering is still ongoing. Otherwise
			// there will be a dangling promises that breaks tests (probably not an actual app)
			if (!renderingComplete) {
				next = promiseWithResolvers();
			}

			// If an error occurs during rendering, throw the error as we cannot proceed.
			if (error) {
				throw error;
			}

			// Get the total length of all arrays.
			let length = 0;
			let stringToEncode = '';
			for (let i = 0, len = buffer.length; i < len; i++) {
				const bufferEntry = buffer[i];

				if (typeof bufferEntry === 'string') {
					const nextIsString = i + 1 < len && typeof buffer[i + 1] === 'string';
					stringToEncode += bufferEntry;
					if (!nextIsString) {
						const encoded = encoder.encode(stringToEncode);
						length += encoded.length;
						stringToEncode = '';
						buffer[i] = encoded;
					} else {
						buffer[i] = '';
					}
				} else {
					length += bufferEntry.length;
				}
			}

			// Create a new array with total length and merge all source arrays.
			let mergedArray = new Uint8Array(length);
			let offset = 0;
			for (let i = 0, len = buffer.length; i < len; i++) {
				const item = buffer[i];
				if (item === '') {
					continue;
				}
				mergedArray.set(item as Uint8Array, offset);
				offset += item.length;
			}

			// Empty the array. We do this so that we can reuse the same array.
			buffer.length = 0;


Review comment (refactor):
I guess this is the bulk of the improvement right? If so a comment here explaining why this seemingly more complicated code is actually good/necessary would help prevent accidental regressions from those who think they are simplifying code. Thanks!

Code after:
				await next.promise;
			}

			// Only create a new promise if rendering is still ongoing. Otherwise
			// there will be a dangling promises that breaks tests (probably not an actual app)
			if (!renderingComplete) {
				next = promiseWithResolvers();
			}

			// If an error occurs during rendering, throw the error as we cannot proceed.
			if (error) {
				throw error;
			}

			// This calculates the length of the final merged array.
			// While doing so, it also replaces consecutive strings with their
			// concatenated Uint8Array equivalent.
			// This is a performance optimization since `TextEncoder#encode` can be
			// costly, so it is faster to encode one larger string than it is
			// to encode many smaller strings.
			let length = 0;
			let stringToEncode = '';
			for (let i = 0, len = buffer.length; i < len; i++) {
				const bufferEntry = buffer[i];

				if (typeof bufferEntry === 'string') {
					const nextIsString = i + 1 < len && typeof buffer[i + 1] === 'string';
					stringToEncode += bufferEntry;
					if (!nextIsString) {
						const encoded = encoder.encode(stringToEncode);
						length += encoded.length;
						stringToEncode = '';
						buffer[i] = encoded;
					} else {
						buffer[i] = '';
					}
				} else {
					length += bufferEntry.length;
				}
			}

			// Create a new array with total length and merge all source arrays.
			let mergedArray = new Uint8Array(length);
			let offset = 0;
			for (let i = 0, len = buffer.length; i < len; i++) {
				const item = buffer[i];
				// If an item is an empty string, it must've been cleared out earlier
				// when we converted it into a larger Uint8Array. Thus, we can skip it.
				if (item === '') {
					continue;
				}