Identify and eliminate redundant computations, function calls, and expensive operations that can be cached, memoized, or combined. This includes avoiding repeated function invocations with the same parameters, caching expensive calculations, and combining multiple operations that access the same data.
Examples of optimization opportunities:
getComponentDef(instance.constructor)
multiple times, store the result in a variableisAnimationRunning
and cancelAnimations
functions to avoid duplicate animation lookups// Before: Redundant function calls
function componentUsesShadowDomEncapsulation(lView: LView): boolean {
const instance = lView[CONTEXT];
return instance?.constructor
? getComponentDef(instance.constructor)?.encapsulation === ViewEncapsulation.ShadowDom ||
getComponentDef(instance.constructor)?.encapsulation === ViewEncapsulation.IsolatedShadowDom
: false;
}
// After: Cache the result
function componentUsesShadowDomEncapsulation(lView: LView): boolean {
const instance = lView[CONTEXT];
if (!instance?.constructor) return false;
const componentDef = getComponentDef(instance.constructor);
return componentDef?.encapsulation === ViewEncapsulation.ShadowDom ||
componentDef?.encapsulation === ViewEncapsulation.IsolatedShadowDom;
}
This optimization reduces computational overhead, improves runtime performance, and can significantly impact applications with frequent function calls or expensive operations.
Enter the URL of a public GitHub repository