Back to all reviewers

Optimize critical path iterations

nestjs/nest
Based on 3 comments
TypeScript

Use appropriate iteration techniques in performance-critical code paths to minimize overhead and maximize execution speed: 1. **Use raw for loops with early termination** instead of array methods like `map` and `filter` for maximum performance:

Performance Optimization TypeScript

Reviewer Prompt

Use appropriate iteration techniques in performance-critical code paths to minimize overhead and maximize execution speed:

  1. Use raw for loops with early termination instead of array methods like map and filter for maximum performance:
// Less performant
return methods
  .map(methodKey => this.exploreMethodMetadata(instance, instancePrototype, methodKey))
  .filter(route => route !== undefined);

// More performant
const routes = [];
for (const methodKey of methods) {
  const route = this.exploreMethodMetadata(instance, instancePrototype, methodKey);
  if (!route) continue;
  routes.push(route);
}
return routes;
  1. Consider reduce as a middle ground between readability and performance when appropriate.

  2. Move invariant calculations outside loops and callbacks to prevent redundant executions:

// Less performant
function callback() {
  const isTreeDurable = wrapper.isDependencyTreeDurable(); // Executed on each call
  // ...
}

// More performant
const isTreeDurable = wrapper.isDependencyTreeDurable(); // Executed once
function callback() {
  // ...
}
  1. Break loops early when the goal has been achieved:
// Less performant (processes all items)
return this.getAll(metadataKey, targets).find(item => item !== undefined);

// More performant (stops at first match)
for (const target of targets) {
  const result = this.get(metadataKey, target);
  if (result !== undefined) {
    return result;
  }
}
return undefined;

For critical code paths, benchmark different approaches with realistic data volumes to confirm performance benefits before sacrificing readability.

3
Comments Analyzed
TypeScript
Primary Language
Performance Optimization
Category

Source Discussions