Documentation Index
Fetch the complete documentation index at: https://mintlify.com/akbarsaputrait/ngememoize/llms.txt
Use this file to discover all available pages before exploring further.
What is memoization?
Memoization is an optimization technique that stores the results of expensive function calls and returns the cached result when the same inputs occur again. Instead of recalculating the result every time, the function checks if it has already computed the result for the given arguments and returns it immediately. Think of it like a smart lookup table: the first time you call a function with specific arguments, it calculates and stores the result. Every subsequent call with the same arguments simply retrieves the stored result.How Ngememoize implements memoization
Ngememoize provides a decorator-based approach to memoization specifically designed for Angular applications. When you apply the@Ngememoize() decorator to a method, the library automatically:
- Generates a unique cache identifier based on the class name and method name
- Creates or retrieves a cache from the
NgememoizeService - Generates a cache key from the method arguments
- Checks if a cached result exists for that key
- Returns the cached result or executes the method and stores the result
ClassName_methodName, ensuring that each method has its own isolated cache space.
Cache hits vs cache misses
Understanding cache behavior is crucial for optimizing performance:Cache hit
A cache hit occurs when the requested data is found in the cache. This is the ideal scenario:- The cached value is returned immediately
- No computation is performed
- A hit is recorded in the statistics (see
ngememoize.service.ts:45-50)
Cache miss
A cache miss occurs when the requested data is not in the cache or has expired:- The original method executes
- The result is stored in the cache with a timestamp
- A miss is recorded in the statistics (see
ngememoize.service.ts:56-61)
Ngememoize only caches truthy values and non-empty arrays. If your method returns
null, undefined, false, or an empty array, the result won’t be cached.When to use memoization
Memoization is beneficial when:Computationally expensive operations
Pure functions with repeated calls
Functions that always return the same output for the same input:Data transformations in templates
When the same transformation is called multiple times during change detection:When NOT to use memoization
Avoid memoization in these scenarios:Functions with side effects
Frequently changing inputs
If your function is rarely called with the same arguments, memoization adds overhead without benefits:Simple, fast operations
The overhead of caching may exceed the cost of the operation:Use the
debugLabel option during development to monitor cache hits and misses, helping you determine if memoization is beneficial for your use case.Performance benefits
Memoization can dramatically improve performance when used correctly. Consider this real-world example:- First call: Filters and sorts 1,000 products (~15ms)
- Subsequent calls (same arguments): Returns cached result (~0.1ms)
- Performance gain: 150x faster for cache hits
- The same computation is triggered multiple times during a single change detection cycle
- The method is called from templates that re-render frequently
- The operation involves complex data transformations or calculations