Memo Decorator In Angular Pipes

Memo Decorator

 
What is Memo Decorator?
  • It is an open-source platform that enhances the caching of pipe’s transform() function.
  • It works with only primitive value types.
  • It checks the value being passed and catches the response. If the same values are passed again, it will return the calculation from the cached result.
  • It is extremely helpful when complex logic is being calculated based on the table’s rows. Using these decorators, we can completely avoid the duplicate for the same input again and again.
For more details, please check here.
 
Let's jump into the code part and see how it works. Follow the steps mentioned below. 
 
Step 1
 
Install the NPM package.
  1. npm i memo-decorator --save  
Step 2
 
Create two pipes to demonstrate the difference between a normal pipe and the memo pipe.
  1. ng g p salaryCalculation    
  2. ng g p dateFormat    
  3. import memo from 'memo-decorator';     
Step 3
 
Now, add some logic to your pipe, as below, with memo decorator.
  1. import { Pipe, PipeTransform } from '@angular/core';  
  2. import memo from 'memo-decorator';  
  3. @Pipe({  
  4.   name: 'salaryCalculation'  
  5. })  
  6. export class SalaryCalculationPipe implements PipeTransform {  
  7.   
  8.   @memo()  
  9.   transform(month: number): number {  
  10.     return this.getSalary(month);  
  11.   }  
  12. getSalary(month: number) {  
  13.   console.log('memo pipe called');  
  14.   return month * 4000;  
  15. }  
  16. }  
This is the code for the other pipe without memo decorator; just understand the difference between it and the memo pipe.
  1. import { Pipe, PipeTransform } from '@angular/core';  
  2. import { DatePipe } from '@angular/common';  
  3.   
  4. @Pipe({  
  5.   name: 'dateFormat'  
  6. })  
  7. export class DateFormatPipe implements PipeTransform {  
  8.   
  9.   transform(date: Date): any {  
  10.    console.log('DateFormatPipe called');  
  11.    const datePipe = new DatePipe('en-US');  
  12.    return datePipe.transform(date, 'dd/MM/yyyy');  
  13.   }  
  14. }  
Step 4
 
Create one sample component to test the memo decorator pipe and normal pipe. So, add this code. 
  1.  sampleData: { name: string; month: number;  ViewDate: Date }[];  
  2.   
  3.   ngOnInit() {  
  4.     this.sampleData = [{name: 'Rahul', month: 5, ViewDate: new Date() },  
  5.     {name: 'Rajendra', month: 2, ViewDate: new Date()},  
  6.     {name: 'Annu', month: 9, ViewDate: new Date()},  
  7.     {name: 'Aishu', month: 3, ViewDate: new Date()},  
  8.     {name: 'Raj', month: 5, ViewDate: new Date()}  
  9.    ];  
  10. }  

Step 5

Given below is the HTML code snippet to loop through all the array items
  1. <div clas="row">  
  2. <ul *ngIf="sampleData">  
  3.   <li *ngFor="let user of sampleData">  
  4.        <p>{{ user.name }} - {{ user.month | salaryCalculation  }} - {{user.ViewDate| dateFormat}}</p>  
  5.   </li>  
  6. </ul>  
  7. </div>  

In this poc, I have a small array object with some data as you can see in step 4 - basic name, month, and date. Here, I'm going to apply the pipes which we created above and find out the difference and benefits of using memo decorators. Let's run it and see the output. I have added a console log to each pipe to see how many times it's called.

It is the browser output.
 Memo Decorator In Angular Pipe's
Let's check the console output.
Memo Decorator In Angular Pipe's 
I hope you got the difference. The non-memo pipe was called 5 times. It was called as per the records in an array while the memo pipe was called only four times even when the row count is 5 . The reason is that Rahul and Raj have the same month data. So, our memo decorator ignored the salary calculation as it already calculated for Rahul, so it returned the cached response value for Raj when the same month was received for calculation.
 

Conclusion

 
Memo Decorators are very useful when it comes to an enterprise application where we do a lot of business logic operations performed on each row or transaction. It completely avoids the unnecessary calculation and helps us to boost the application performance.
 
Let me know if you have any questions or suggestions. 
Next Recommended Reading Angular 7 Table Reusable Sorting Pipe