Identify and cache results of computationally expensive or repetitive operations instead of recalculating them multiple times. This includes function calls, date object creation, data transformations, and resolved promises.
Identify and cache results of computationally expensive or repetitive operations instead of recalculating them multiple times. This includes function calls, date object creation, data transformations, and resolved promises.
Examples of operations to cache:
// Before: Inefficient - recalculating for each prompt
await Promise.all(
resolvedPrompts.map(async (prompt) =>
promptChangeEventSourcing(
await promptService.resolvePrompt(prompt), // Redundant async call
// After: Efficient - reusing already resolved data
await Promise.all(
resolvedPrompts.map(async (prompt) =>
promptChangeEventSourcing(
prompt, // Using already resolved prompt
// Before: Inefficient - repeated function call
const maxTemperature = getDefaultAdapterParams(selectedProviderApiKey.adapter).maxTemperature;
const temperature = getDefaultAdapterParams(selectedProviderApiKey.adapter).temperature;
const maxTokens = getDefaultAdapterParams(selectedProviderApiKey.adapter).max_tokens;
// After: Efficient - computing once and reusing
const adapterParams = getDefaultAdapterParams(selectedProviderApiKey.adapter);
const maxTemperature = adapterParams.maxTemperature;
const temperature = adapterParams.temperature;
const maxTokens = adapterParams.max_tokens;
Look for patterns where the same operation is performed repeatedly with the same inputs, especially in loops, map functions, or closely related code blocks.
Enter the URL of a public GitHub repository