Back to all reviewers

Early returns prevent waste

facebook/react-native
Based on 5 comments
JavaScript

Add early return statements and duplicate checks to avoid unnecessary computations, memory allocations, and API calls. This pattern prevents performance degradation by stopping expensive operations before they begin.

Performance Optimization JavaScript

Reviewer Prompt

Add early return statements and duplicate checks to avoid unnecessary computations, memory allocations, and API calls. This pattern prevents performance degradation by stopping expensive operations before they begin.

Key strategies:

  1. Check for duplicates before adding to collections to prevent memory leaks
  2. Validate preconditions early to avoid unnecessary processing
  3. Return immediately when work is not needed instead of continuing execution

Example implementation:

__addChild(child: AnimatedNode): void {
  // Prevent adding duplicate animated nodes
  if (this._children.includes(child)) {
    return;
  }
  // Continue with expensive operations only if needed
  this._children.push(child);
  // ... rest of implementation
}

_maybeCallOnEndReached() {
  const {onEndReached} = this.props;
  // Early return if callback doesn't exist
  if (!onEndReached) {
    return;
  }
  // Early return if scrolling in wrong direction
  if (offset <= 0 || dOffset <= 0) {
    return;
  }
  // ... continue with expensive calculations
}

This approach reduces unnecessary memory allocations, prevents duplicate processing, and eliminates redundant API calls, leading to better overall performance and resource utilization.

5
Comments Analyzed
JavaScript
Primary Language
Performance Optimization
Category

Source Discussions