[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: ...
随机推荐
- 简单layer 快速上手
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 从零开始学Flask框架-006
重定向和用户会话 因为刷新页面时浏览器会重新发送之前已经发送过的最后一个请求.如果这个请求是一个包含表单数据的POST 请求,刷新页面后会再次提交表单. 基于这个原因,最好别让Web 程序把POST ...
- Shiro身份认证、盐加密
目的: Shiro认证 盐加密工具类 Shiro认证 1.导入pom依赖 <dependency> <groupId>org.apache.shiro</groupId& ...
- shellexecute的使用和X64判断
bool RunConsoleAsAdmin(std::string appPath, std::string param, bool wait) { LOG_INFO << " ...
- 使用VS2012编译和使用C++ STL(STLport)
使用VS2012编译和使用C++ STL(STLport) http://cstriker1407.info/blog/use-vs2012-to-compile-and-use-the-c-stl- ...
- weui中的picker滑动报错
html { touch-action: none; } 在页面插入上述代码即可解决
- SVM-支持向量机总结
一.SVM简介 (一)Support Vector Machine 支持向量机(SVM:Support Vector Machine)是机器学习中常见的一种分类算法. 线性分类器,也可以叫做感知机,其 ...
- FreeRTOS 任务创建和删除(静态)
#define configSUPPORT_STATIC_ALLOCATION 1 //打开静态方法 StackType_t TaskStackBuffer[50]; //任务堆栈大小 StaticT ...
- 微信小程序错误readFile:fail parameter error: parameter.filePath should be String instead of Undefined;
我是在使用camera组件时遇到的该问题 原因是未保存文件路径(微信使用摄像头拍照后会把图片保存在一个临时的路径,所以你需要自己定义一个变量来存这个路径,以备下次使用该变量去访问文件) 所以加上你需要 ...
- arm9交叉编译工具链
Arm-linux-gcc: gcc和arm-linux-gcc的头文件并不一样. Eg. Arm-linux-ld:链接器,-T参数是使用链接器脚本. Eg. Arm-linux-readelf:读 ...