
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
ngx-memoize
Advanced tools
A customized decorator to memoize methods in Angular templates, preventing unnecessary re-execution. Supports Auto-Destroy and Zoneless.
A lightweight, zero-dependency TypeScript decorator tailored for Angular applications. It memoizes class methods to optimize performance, especially when calling methods directly from templates.
It effectively solves the performance overhead caused by Angular's Change Detection calling template methods repeatedly.
In Angular, binding a method in a template (e.g., {{ calculate(value) }}) can be costly because Angular re-executes the method on every Change Detection cycle.
ngx-memoize ensures the method is executed only when its arguments change, caching the result for subsequent calls with the same arguments.
ngOnDestroy), preventing memory leaks.npm install ngx-memoize
Simply apply the @Memoize() decorator to your method.
import { Component } from "@angular/core";
import { Memoize } from "ngx-memoize";
@Component({
selector: "app-user-profile",
template: `
<!-- Without Memoize, this runs on every CD cycle.
With Memoize, it runs ONLY when user.id changes. -->
<p>User Initial: {{ getInitial(user.name) }}</p>
`,
})
export class UserProfileComponent {
user = { id: 1, name: "Alice" };
@Memoize()
getInitial(name: string): string {
console.log("Calculating initial..."); // Logs only when 'name' changes
return name.charAt(0).toUpperCase();
}
}
By default, the decorator automatically hooks into the Angular lifecycle. When your component is destroyed (e.g., user navigates away), all cached results for that instance are immediately cleared from memory.
You don't need to do anything extra. It just works.
@Memoize() // autoDestroy is true by default
heavyCalculation(val: number) {
// ...
}
// ⚠️ IMPORTANT:
// For Angular to trigger the destroy hook (especially in AOT builds),
// your class MUST implement ngOnDestroy, even if it's empty.
ngOnDestroy() {}
In some cases, you might want to disable automatic cleanup (e.g., in a Singleton Service that never destroys) or you need to clear the cache manually while the component is still alive.
You can disable the auto-cleanup behavior by passing { autoDestroy: false }. Warning: If you do this, you become responsible for clearing the cache to avoid memory leaks.
import { Memoize, clearMemoized } from "ngx-memoize";
@Injectable()
export class MyService implements OnDestroy {
// Disable auto destroy because this service might live longer than expected
// or you want to handle cleanup yourself.
@Memoize({ autoDestroy: false })
expensiveOperation(data: any) {
// ...
}
// When YOU decide it's time to clean up:
ngOnDestroy() {
clearMemoized(this);
}
}
Even with autoDestroy: true, you might want to force a refresh (e.g., if data inside the service changed but the arguments passed to the method didn't).
export class DataComponent {
@Memoize()
processData(id: number) {
return heavyStuff(id);
}
forceRefresh() {
// Option A: Clear cache for a SPECIFIC method on this instance
clearMemoized(this, "processData");
// Option B: Clear cache for ALL methods on this instance
clearMemoized(this);
console.log("Cache cleared! Next call will re-calculate.");
}
}
ngOnDestroy. When Angular calls ngOnDestroy, the decorator intercepts it and deletes all cached properties from the instance, then calls your original ngOnDestroy.MIT
FAQs
A customized decorator to memoize methods in Angular templates, preventing unnecessary re-execution. Supports Auto-Destroy and Zoneless.
We found that ngx-memoize demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.