[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 ...
随机推荐
- php write excel
/** * 写excel方法 */ function writeExcel($tabArr, $dataArr,$path) { require_once CODE_BASE2 . '/util/ph ...
- HDU3533 Escape
题目: The students of the HEU are maneuvering for their military training. The red army and the blue a ...
- POJ 2823 线段树 Or 单调队列
时限12s! 所以我用了线段树的黑暗做法,其实正解是用单调队列来做的. //By SiriusRen #include <cstdio> #include <cstring> ...
- Php.ini文件位置在哪里 Php.ini文件找不到
转载自:http://www.php100.com/html/php/rumen/2013/0831/26.html [导读] Php ini文件是php的一个配置文件,在windows主机中如果你未 ...
- Spring + Redis ( 简单使用)
1.Redis 的 Java API Java 中 使用 Redis 工具,要先去 maven 仓库中,下载 jedis jar包 jedis 依赖 <dependency> <gr ...
- 运用<body>属性,渲染页面效果
新建一个HTML5文件,为<body>标签添加样式,代码如下: 01 <!doctype html> 02 <html> 03 <head> 04 &l ...
- 【Linux】连接CRT
linux中出现crt连接不上多数是ip地址设置不正确. window中命令行界面(cmd进入),输入ipconfig,查看虚拟机的ip. 打开linux终端,命令行下输入:ifconfig eth0 ...
- JNI传递字符串数组J-StringArray
编译器对语言的转换以寻地址的方式进行序列化和反序列化,因此对于不固定类型或者不显示给出大小的对象不能直接解析,所以没有出现jstringArray这样的类型,只能一个一个编写. 参考链接:安卓开发提高 ...
- c#日期计算
/// <summary> /// 计算日期的间隔(静态类) /// </summary> public static class dateTimeDiff { /// < ...
- mui switch 开关js控制打开 & Cannot read property 'toggle' of null
//打开开关 mui('#mySwitch').switch().toggle(); //小开关打开异常的情况解决办法$(".mui-switch-handle").attr(&q ...