Skip to content

Conversation

@smartlabsAT
Copy link
Owner

Summary

  • Optimize duplicate breakpoint detection from O(n²) to O(n) complexity
  • Fix missing useEffect dependency to prevent stale closures
  • Maintain existing functionality with improved performance

Changes Made

🚀 Performance Optimization

Before: Used filter with findIndex causing O(n²) complexity

const duplicates = sortedBreakpoints.filter(
  ([, value], index) => sortedBreakpoints.findIndex(([, v]) => v === value) !== index
);

After: Use Set-based approach for O(n) complexity

const seenValues = new Set<number>();
const duplicates: Array<[Breakpoint, number]> = [];

for (const entry of sortedBreakpoints) {
  const [, value] = entry;
  if (seenValues.has(value)) {
    duplicates.push(entry);
  } else {
    seenValues.add(value);
  }
}

🔧 Bug Fix

Before: Missing dependency in useEffect

}, [sortedBreakpoints]);

After: Complete dependency array

}, [sortedBreakpoints, shouldLog]);

Benefits

  • Performance: Significantly better performance with larger breakpoint arrays
  • Correctness: Prevents stale closure issues when shouldLog changes
  • Maintainability: Cleaner, more explicit code
  • Testing: All existing tests continue to pass

Quality Checks

  • ✅ All tests pass (5/5)
  • ✅ ESLint validation passed
  • ✅ TypeScript compilation successful
  • ✅ Pre-commit hooks passed (lint, typecheck, build, test)

Test plan

  • Existing functionality verified through test suite
  • Performance improvement confirmed (O(n) vs O(n²))
  • No regressions in duplicate detection logic
  • useEffect dependency fix prevents potential closure issues

Fixes #4

- Replace O(n²) filter with findIndex with O(n) Set-based duplicate detection
- Add missing shouldLog dependency to useEffect array to prevent stale closures
- Improve performance for larger breakpoint configurations
- Maintain same functionality with better algorithmic complexity

Fixes #4
@smartlabsAT smartlabsAT merged commit b5a49fb into main Oct 6, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize duplicate breakpoint detection and fix useEffect dependencies

2 participants