Identify and optimize frequently executed code paths by moving invariant calculations and conditional logic outside of loops, recursive functions, and other performance-critical sections. This reduces repeated evaluations and improves algorithm efficiency.
Identify and optimize frequently executed code paths by moving invariant calculations and conditional logic outside of loops, recursive functions, and other performance-critical sections. This reduces repeated evaluations and improves algorithm efficiency.
For example, instead of this:
if (query.sort) {
const collator = new Intl.Collator();
filtered = filtered.sort((a, b) => {
if (query.sort === 'alpha-asc') {
return collator.compare(a.title, b.title);
}
if (query.sort === 'alpha-desc') {
return collator.compare(b.title, a.title);
}
});
}
Prefer this:
if (query.sort) {
const collator = new Intl.Collator();
const mult = query.sort === 'alpha-desc' ? -1 : 1;
filtered = filtered.sort((a, b) => mult * collator.compare(a.title, b.title));
}
This principle applies broadly to:
When optimizing recursive algorithms, also ensure you have proper termination conditions to prevent infinite recursion, particularly when dealing with self-referential data structures.
Enter the URL of a public GitHub repository