[Angular] Increasing Performance by using Pipe
For example you make a function to get rating;
getRating(score: number): string {
let rating: string;
console.count('RatingPipe');
if(score > 249000){
rating = "Daniel Boone";
}
else if(score > 200000){
rating = "Trail Guide";
}
else if(score > 150000){
rating = "Adventurer";
}
else if(score > 100000){
rating = "Pioneer";
}
else if(score > 50000){
rating = "Greenhorn";
}
else{
rating = "Buzzard food";
}
return rating;
}
Then using it in html:
{{getRating(entry.points)}}
These code actually casues the preformance issues, because everything Angualr's change detection run, it saw function call inside {{}}, it have to run it everything when anything changes, there is no way to figure out whether the function output changes or not without running it.
The way to fix it is using Pipe. Angular will remember the input value and cache the output. Therefore by using pipe we can reduce the number of function call way better.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'Rating'
})
export class ScoreRatingPipe implements PipeTransform {
transform(score: number): string {
let rating: string;
console.count('RatingPipe');
if(score > 249000){
rating = "Daniel Boone";
}
else if(score > 200000){
rating = "Trail Guide";
}
else if(score > 150000){
rating = "Adventurer";
}
else if(score > 100000){
rating = "Pioneer";
}
else if(score > 50000){
rating = "Greenhorn";
}
else{
rating = "Buzzard food";
}
return rating;
}
}
{{entry.points | Rating }}
[Angular] Increasing Performance by using Pipe的更多相关文章
- Increasing Performance by Reducing Dynamic Dispatch
https://developer.apple.com/swift/blog/?id=39 Increasing Performance by Reducing Dynamic Dispatch Li ...
- [Angular 2] Understanding Pure & Impure pipe
First, how to use a build in pipe: <div class="pipe-example"> <label>Uppercase ...
- [Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword
Angular allows us to conveniently use the async pipe to automatically register to RxJS observables a ...
- angular和ionic4对过滤器pipe的使用
以下为自定义过滤器 import { Pipe, PipeTransform, Injectable } from '@angular/core'; import { DomSanitizer} fr ...
- [Angular] Some performance tips
The talk from here. 1. The lifecycle in Angular component: constructor vs ngOnInit: Constructor: onl ...
- CVE-2017-7494 Linux Samba named pipe file Open Vul Lead to DLL Execution
catalogue . 漏洞复现 . 漏洞代码原理分析 . 漏洞利用前提 . 临时缓解 && 修复手段 1. 漏洞复现 . SMB登录上去 . 枚举共享目录,得到共享目录/文件列表,匿 ...
- Angular2 Pipe
AngularJs 1.x 中使用filters来帮助我们转换templates中的输出,但在Angular2中使用的是pipes,以下展示Angular 1.x and Angular 2中filt ...
- 转:一个C语言实现的类似协程库(StateThreads)
http://blog.csdn.net/win_lin/article/details/8242653 译文在后面. State Threads for Internet Applications ...
- state Threads 开源库介绍
译文在后面. State Threads for Internet Applications Introduction State Threads is an application library ...
随机推荐
- [Apple开发者帐户帮助]五、管理标识符(1)注册应用程序ID
一个应用程序ID标识的配置设定档中的应用程序.它是一个由两部分组成的字符串,用于标识来自单个开发团队的一个或多个应用程序.有两种类型的应用程序ID:用于单个应用程序的显式应用程序ID,以及用于一组应用 ...
- selenium3 + python - table定位
前言 在web页面中经常会遇到table表格,特别是后台操作页面比较常见.本篇详细讲解table表格如何定位. 一.认识table 1.首先看下table长什么样,如下图,这种网状表格的都是table ...
- 如何让 vue 在 sublime 中变成彩色的
在 sublime 中编辑 vue 时,导入后是纯白色的文本,如下图: 想让其变成彩色的文本,需要安装插件,步骤如下: 1. 按住:Ctrl + Alt + P 2. 输入:install Packa ...
- MyEclipse中快速复制粘贴当前行的操作
- Linux scp 后台运行传输文件
Linux scp 设置nohup后台运行 1.正常执行scp命令 2.输入ctrl + z 暂停任务 3.bg将其放入后台 4.disown -h 将这个作业忽略HUP信号 5.测试会话中断,任务继 ...
- iOS-如何返回某个字符串的拼音助记码
我也是看了网上的一个示例代码后,在它的基础上进行的修改.因为项目上会用到,我相信很多人的项目上也会用到.所以实现后,也赶紧分享出来,希望后来人不需要花费时间了. 提示:这里用到了正则表达式,使用了一个 ...
- 详解CorelDRAW智能填充工具的运用
使用智能填充工具可以为任意的闭合区域填充颜色并设置轮廓.与其他填充工具不同,智能填充工具仅填充对象,它检测到区域的边缘并创建一个闭合路径,因此可以填充区域.例如,智能填充工具可以检测多个对象相交产生的 ...
- 前端工具gulp
最近在写一个新的项目,用到了新框架,主要是:react+webpack.里面还用到了一个前端工具——gulp. gulp在项目里的作用是打包静态资源.编译less,压缩css等.js并不在处理之列(不 ...
- 插入DOM元素
插入Dom元素两种情况: 1.要插入的元素是从页面中获取的dom结构 ,例如:$(".item") 2.要插入的元素是通过变量存储的dom结构,例如:var html = &quo ...
- Tomcat Eclipse Debug出现异常
1.可能是java类没有及时更新成class文件2.本地程序没有同步到Tommcat服务器里面3.Servlet类里面加了版本号private static final long serialVers ...