[Angular] Using Pipe for function memoization
Sometimes we might have some expensive function to calcuate state directly from template:
<div class="chip">
{{ calculate (item.num)}}
</div>
calculate(num: number) {
return fibonacci(num);
}
The ´calculate´ function is a pure function, we can use memoization to imporve the profermance. Angualr pure Pipe is good match for that:
// calculate.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
const fibonacci = (num: number): number => {
if (num === || num === ) {
return ;
}
return fibonacci(num - ) + fibonacci(num - );
};
@Pipe({
name: 'calculate'
})
export class CalculatePipe implements PipeTransform {
transform(num: number): any {
return fibonacci(num);
}
}
<div class="chip">
{{ item.num | calculate }}
</div>
If we call 'calcualate' with the same params, it will return the cached value.
[Angular] Using Pipe for function memoization的更多相关文章
- [Angular 2] Pipe Purity
Explaining how Pipes only change by default when your Pipe input parameters change and not when your ...
- [Angular] Increasing Performance by using Pipe
For example you make a function to get rating; getRating(score: number): string { let rating: string ...
- [Angular 2] Understanding Pure & Impure pipe
First, how to use a build in pipe: <div class="pipe-example"> <label>Uppercase ...
- [Angular Unit Testing] Testing Pipe
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'filesize' }) export class FileSi ...
- [Angular] Create a custom pipe
For example we want to create a pipe, to tranform byte to Mb. We using it in html like: <div> ...
- angular和ionic4对过滤器pipe的使用
以下为自定义过滤器 import { Pipe, PipeTransform, Injectable } from '@angular/core'; import { DomSanitizer} fr ...
- Angular 利用 marked.js 添加 Markdown + HTML 同时渲染的 Pipe
背景 最近在公司开发的一个项目需要在 Angular 上展示图文,并且需要同时支持 Markdown 和 HTML 对于同时支持 Markdown 和 HTML ,应该要分为编辑和渲染两部分考虑. 对 ...
- gulp + angular + requirejs 简单学习
一.安装gulp(已经安装了node环境) npm install -g gulp 二.在package.json文件中配置依赖插件 { "name": "xxxx&q ...
- vue,react,angular三大web前端流行框架简单对比
常用的到的网站 vue学习库: https://github.com/vuejs/awesome-vue#carousel (json数据的格式化,提高本地测试的效率) json在线编辑: http: ...
随机推荐
- hdu 6609 区间条件前缀和 + 二分
题目传送门//res tp hdu 目的 在尾部逐步插入n个元素,求插入第i个元素时,[1,i)内删去多少个元素,可使前缀和[1,i]不大于m 多测Q [1,15] n [1,2e5] m [1,1e ...
- input的小技巧 清除自动记录
input 消除自动记忆功能 在html里就可以直接清除了<input type="text" autocomplete="off"> input ...
- 100天搞定机器学习|day54 聚类系列:层次聚类原理及案例
几张GIF理解K-均值聚类原理 k均值聚类数学推导与python实现 前文说了k均值聚类,他是基于中心的聚类方法,通过迭代将样本分到k个类中,使每个样本与其所属类的中心或均值最近. 今天我们看一下无监 ...
- 【字符串hash】DNA
DNA 题目描述 小X身为奆老,兴趣爱好广泛,他还非常喜欢研究DNA序列……小X进行了一项关于DNA序列研究,发现人某条染色体上的一段DNA序列中连续的k个碱基组成的碱基序列与做题的AC率有关!于是他 ...
- Kubernetes 常见错误
OOMKilled: Pod 的内存使用超出了 resources.limits 中的限制,被强制杀死. CrashLoopBackoff: Pod 进入 崩溃-重启循环,重启间隔时间从 10 20 ...
- mybatis执行DDL语句
对MyBatis一直停留在仅仅会用的阶段,常用的场景就是通过MyBatis对表数据进行DML(insert, delete, update等)操作,从来没有想过通过MyBatis对数据库进行DDL(c ...
- 在浏览器输入URL回车后发生了什么?
本文由 简悦 SimpRead 转码, 原文地址 https://4ark.me/post/b6c7c0a2.html 这个问题已经是老生常谈了,更是经常被作为面试的压轴题出现,网上也有很多文章,但最 ...
- springboot 常见的启动器
<!--pringBoot提供了一个名为spring-boot-starter-parent的工程, 里面已经对各种常用依赖(并非全部)的版本进行了管理 我们的项目需要以这个项目为父工程,这样我 ...
- Windows下常用DOS命令
1.添加用户命令: net user 用户名 密码 /add 2.将用户加入组的命令: net localgroup administrators 用户名 /add 3.在dos命令行模式下启用用户: ...
- el-table——可编辑、拖拽排序与校验的formTableDrag
背景: 1.利用form进行校验输入: 2.利用sortable操作Dom替换表格数据顺序: 3.利用lodash实现数据深拷贝与参数替换等 一:最外层的数组校验 <template> & ...