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的更多相关文章

  1. [Angular 2] Pipe Purity

    Explaining how Pipes only change by default when your Pipe input parameters change and not when your ...

  2. [Angular] Increasing Performance by using Pipe

    For example you make a function to get rating; getRating(score: number): string { let rating: string ...

  3. [Angular 2] Understanding Pure & Impure pipe

    First, how to use a build in pipe: <div class="pipe-example"> <label>Uppercase ...

  4. [Angular Unit Testing] Testing Pipe

    import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'filesize' }) export class FileSi ...

  5. [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> ...

  6. angular和ionic4对过滤器pipe的使用

    以下为自定义过滤器 import { Pipe, PipeTransform, Injectable } from '@angular/core'; import { DomSanitizer} fr ...

  7. Angular 利用 marked.js 添加 Markdown + HTML 同时渲染的 Pipe

    背景 最近在公司开发的一个项目需要在 Angular 上展示图文,并且需要同时支持 Markdown 和 HTML 对于同时支持 Markdown 和 HTML ,应该要分为编辑和渲染两部分考虑. 对 ...

  8. gulp + angular + requirejs 简单学习

    一.安装gulp(已经安装了node环境) npm  install -g gulp 二.在package.json文件中配置依赖插件 { "name": "xxxx&q ...

  9. vue,react,angular三大web前端流行框架简单对比

    常用的到的网站 vue学习库: https://github.com/vuejs/awesome-vue#carousel (json数据的格式化,提高本地测试的效率) json在线编辑: http: ...

随机推荐

  1. [转帖]nginx sendfile tcp_nopush tcp_nodelay参数解释

    nginx sendfile tcp_nopush tcp_nodelay参数解释 2013-06-25 13:59:40 zmj_88888888 阅读数 20425 文章标签: nginxtcp_ ...

  2. Javascript 闭包何时回收?

    定义 闭包是函数和声明该函数的词法环境的组合.闭包是指有权访问另一个函数作用域中的变量的函数,创建闭包的最常见的方式就是在一个函数内创建另一个函数,通过另一个函数访问这个函数的局部变量. 范例 fun ...

  3. mysql查看索引的大小

    1 ,show table status from your_database_name  这个不准: 2,这个比较详细 select database_name, table_name, index ...

  4. PAT(B) 1079 延迟的回文数(Java)

    题目链接:1079 延迟的回文数 (20 point(s)) 题目描述 给定一个 k+1 位的正整数 N,写成 a​k​​⋯a​1​​a​0​​ 的形式,其中对所有 i 有 0≤a​i​​<10 ...

  5. svn钩子(hooks)

    目录 钩子脚本的具体写法就是操作系统中shell脚本程序的写法,请根据自己SVN所在的操作系统和shell程序进行相应的写作 所谓钩子就是与一些版本库事件触发的程序,例如新修订版本的创建,或是未版本化 ...

  6. 网易自动化测试工具(airtest)的环境部署

    airtest 环境配置: 1.安装Python2.7 及 Python3.6 版本(2个需要都安装) 2.配置python环境变量(AirtestIDE 需要在python2.x的环境下运行,所以尽 ...

  7. 最简容器动手小实践——FC坦克大战游戏容器化

    FC 经典力作相信大家一点也不陌生.童年时期最频繁的操作莫过于跳关,在 果断跳到最后一关之后,一般都是以惨败告终,所以还是一关一关的过原始积累才能笑到最后.这款游戏的经典就在于双人配合,守家吃装备.也 ...

  8. springboot中将日志信息存放在catalina.base中

    <?xml version="1.0" encoding="UTF-8"?> <configuration debug="true& ...

  9. robot framework 接口测试 http协议post请求json格式

    robot framework 接口测试 http协议post请求json格式 讲解一个基础版本.注意区分url地址和uri地址. rf和jmeter在添加服务器地址也就是ip地址的时候,只能url地 ...

  10. 基于re模块的计算器

    最终计算器需求: 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - ...