[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: ...
随机推荐
- MYSQL 八大优化方案
1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设 ...
- Linux基础-15-samba服务
1. samba的功能: samba是一个网络服务器,用于Linux和Windows之间共享文件. 2. samba服务的启动.停止.重启 service smb start|stop|restart ...
- Python学习路线图【转载】
文章来源:https://blog.csdn.net/u014044812/article/details/88079011
- bash 和 powershell 常用命令集锦
Linux Shell # 1. 后台运行命令 nohup python xxx.py & # 查找替换 ## 只在目录中所有的 .py 和 .dart 文件中递归搜索字符"main ...
- es6☞对象的解构赋值
变量必须与属性同名 let {name, age} = {name: 'wang', age: 22}; console.log(name, age); //wang 22 let {name} = ...
- Detection综述
4月中旬开始,尝试对目标检测领域做一个了解,看了差不多6-7篇paper,在这里记录一下: 一.Detection简介 人脸检测的目标是找出图像中所有的人脸对应的位置,算法的输出是人脸外接矩形在图像中 ...
- aliyun maven仓库连不上
长时间未使用maven仓库,今天使用发现始终无法下载jar包,连不上阿里云maven仓库. 因为之前一直使用正常,以为是idea maven设置的有问题,再三确认地址无误,maven offline也 ...
- Java 之 ObjectInputStream 类
ObjectInputStream 类 1.概述 java.io.ObjectInputStream extends InputStream ObjectInputStream 反序列化流,将之前使用 ...
- Microsoft Word (2016) Deceptive File Reference ZDI-CAN-7949
[+] Credits: John Page (aka hyp3rlinx) [+] Website: hyp3rlinx.altervista.org[+] Source: http://hyp3 ...
- Python 使用gevent实现多任务
import gevent import time # 如果需要默认的 time.sleep(0.5) 需要打补丁 from gevent import monkey monkey.patch_all ...