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

  1. Increasing Performance by Reducing Dynamic Dispatch

    https://developer.apple.com/swift/blog/?id=39 Increasing Performance by Reducing Dynamic Dispatch Li ...

  2. [Angular 2] Understanding Pure & Impure pipe

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

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

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

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

  5. [Angular] Some performance tips

    The talk from here. 1. The lifecycle in Angular component: constructor vs ngOnInit: Constructor: onl ...

  6. CVE-2017-7494 Linux Samba named pipe file Open Vul Lead to DLL Execution

    catalogue . 漏洞复现 . 漏洞代码原理分析 . 漏洞利用前提 . 临时缓解 && 修复手段 1. 漏洞复现 . SMB登录上去 . 枚举共享目录,得到共享目录/文件列表,匿 ...

  7. Angular2 Pipe

    AngularJs 1.x 中使用filters来帮助我们转换templates中的输出,但在Angular2中使用的是pipes,以下展示Angular 1.x and Angular 2中filt ...

  8. 转:一个C语言实现的类似协程库(StateThreads)

    http://blog.csdn.net/win_lin/article/details/8242653 译文在后面. State Threads for Internet Applications ...

  9. state Threads 开源库介绍

    译文在后面. State Threads for Internet Applications Introduction State Threads is an application library ...

随机推荐

  1. Firefox浏览器中,input输入框输入的内容在刷新网页后为何还在?

    转自:http://www.webym.net/jiaocheng/473.html 这个问题比较容易解决,如果不希望浏览器保留以前输入的内容,只要给对应的 input 输入框加上以下参数: auto ...

  2. diaowen Maven Webapp

    五月 , :: 上午 org.apache.catalina.startup.VersionLoggerListener log INFO: Server version: Apache Tomcat ...

  3. Cash Machine(多重背包)

    http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...

  4. MYSQL 数据库命令行终端操作笔记

    1.数据库登录: 1.登录本地的MYSQL数据库:mysql -u root -p   2.连接远程主机上的MYSQL数据库:mysql -h 192.168.191.2 -u root -p 123 ...

  5. 使用MALTAB标定实践记录

    记录一下整个相机的标定矫正过程,希望能够帮助到刚学习标定的童鞋们- 首先下载matlab calibration toolbox,百度搜索第一条就是了(http://www.vision.caltec ...

  6. sql学习--update

    两种修改形式 第一种:静态插入 ,notes='began career selling ...balabala' where jc='johnny ca' 第二种: --注意别名和on后边的表连接不 ...

  7. CSS命名规则和如何命名

    CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:l ...

  8. Retrofit 传递json 和 复杂参数类型List<T>

    1 首先你要定义一个接口 @POST Call<String> post(@Url String url, @Body String info); 2 创建一个service public ...

  9. 【SQL】BETWEEN操作符

    BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围. 操作符 BETWEEN ... AND 会选取介于两个值之间的数据范围.这些值可以是数值.文本或者日期. 注意: ...

  10. tomcat ider配置

    xml文件配置: <servlet> <servlet-name>test1</servlet-name>//设定java文件链接的锚点 <servlet-c ...