Minimize memory allocations by avoiding intermediate objects, sharing underlying buffers, and eliminating unnecessary array operations. This is particularly important in hot code paths and when processing large datasets.
Key strategies:
Buffer.from(arg.buffer, arg.byteOffset, arg.byteLength)
instead of Buffer.from(arg)
new Uint8Array(value)
instead of new Uint8Array(Buffer.from(value).buffer)
Example of optimization:
// Before: Creates unnecessary intermediate array
const results: Value[] = []
for (const arg of node.args) {
results.push(await this.interpretNode(arg, queryable, scope, generators))
}
return results[results.length - 1]
// After: Only compute what's needed
let lastResult: Value
for (const arg of node.args) {
lastResult = await this.interpretNode(arg, queryable, scope, generators)
}
return lastResult
Enter the URL of a public GitHub repository