使用OnPush和immutable.js来提升angular的性能
angular里面变化检测是非常频繁的发生的,如果你像下面这样写代码
<div>
{{hello()}}
</div>
则每次变化检测都会执行hello函数,如果hello函数十分耗时,则会出现占用CPU高的问题。
这时,推荐使用OnPush策略和immutable.js来提升angular应用的性能。
OnPush策略可以阻止angular变化检测传入组件,这样每次变化检测不会进到你的组件里面来调用hello函数。
引入immutable.js的作用是为了更加方便的使用OnPush策略。
看下immutable.js的例子
const list1 = Immutable.List([ {a: 1}, {a: 2} ]);
const list2 = list1.push({a: '3'});
console.log(list1 === list2); // false
console.log(list1.get(0) === list2.get(0)); // true
const list3 = list2.pop();
console.log(list1 === list3); // false
const map1 = Immutable.Map({ a: 1, b: 2, c: 3 });
const map2 = map1.set('b', 50);
console.log(map1 === map2); // false
const map3 = map1.set('b', 2);
console.log(map1 === map3); // true
const map4 = map2.set('b', 2);
console.log(map1 === map4); // false
这样在父组件里只需要如此写
export class AppComponent {
ii = '';
list = List([{label: 'default'}]);
add() {
if (this.ii) {
this.list = this.list.push({label: this.ii});
this.ii = '';
}
}
}
子组件HTML代码
<div>
{{hello()}}
</div> <ul *ngFor="let item of list">
<li>{{item.label}}</li>
</ul>
不需要trackby,因为默认trackby就是比较元素相等,immutable.js数组里不变的元素就是相等的,少写一些代码啦。
使用OnPush和immutable.js来提升angular的性能的更多相关文章
- 大话immutable.js
为啥要用immutable.js呢.毫不夸张的说.有了immutable.js(当然也有其他实现库)..才能将react的性能发挥到极致!要是各位看官用过一段时间的react,而没有用immutabl ...
- React+Immutable.js的心路历程
这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...
- Immutable.js 以及在 react+redux 项目中的实践
来自一位美团大牛的分享,相信可以帮助到你. 原文链接:https://juejin.im/post/5948985ea0bb9f006bed7472?utm_source=tuicool&ut ...
- 深度浅出immutable.js
这篇文章将讲述immutable.js的基本语法和用法. 1.fromJs() Deeply converts plain JS objects and arrays to Immutable Ma ...
- Immutable.js – JavaScript 不可变数据集合
不可变数据是指一旦创建就不能被修改的数据,使得应用开发更简单,允许使用函数式编程技术,比如惰性评估.Immutable JS 提供一个惰性 Sequence,允许高效的队列方法链,类似 map 和 f ...
- js 变量提升+方法提升
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Immutable.js尝试(node.js勿入)
最近做一些复杂html常常需要在页面做一些数据处理,常常在想如果 js有list 这种数据结构多少,今天逛github时 发现有Immutable.js 这个项目https://github.com/ ...
- [Javascript] Creating an Immutable Object Graph with Immutable.js Map()
Learn how to create an Immutable.Map() through plain Javascript object construction and also via arr ...
- [Javascript] Manage Application State with Immutable.js
Learn how Immutable.js data structures are different from native iterable Javascript data types and ...
随机推荐
- css的flex布局调试
学习经验-css的flex布局 今天遇到一个小问题 在给三个div布局时,设置父元素display:flex 此时三个div的宽度均为50%,他们并没有超出屏幕的宽度,还是撑满了父元素. 为什么呢? ...
- 手脱NsPacK壳
1.查壳 使用PEiD未能检测到壳信息,这时,我们更换其他工具 从图中可以看到壳的信息为[NsPacK(3.x)[-]] 2.百度壳信息 北斗程序压缩(Nspack)是一款压缩壳.主要的选项是:压缩资 ...
- Angular中懒加载一个模块并动态创建显示该模块下声明的组件
angular中支持可以通过路由来懒加载某些页面模块已达到减少首屏尺寸, 提高首屏加载速度的目的. 但是这种通过路由的方式有时候是无法满足需求的. 比如, 点击一个按钮后显示一行工具栏, 这个工具栏组 ...
- 143_Power BI&Power Pivot月度、季度、半年度、全年同维度展示
博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 一.背景 最近在做下一年度的预算,做出来需要月度.季度.半年度.全年都展示出来,在做测算的是时候,默认的透视表已经无法满足 ...
- Java虚拟机启动过程解析
一.序言 当我们在编写Java应用的时候,很少会注意Java程序是如何被运行的,如何被操作系统管理和调度的.带着好奇心,探索一下Java虚拟机启动过程. 1.素材准备 从Java源代码.Java字节码 ...
- 题解0014:信奥一本通1472——The XOR Largest Pair(字典树)
题目链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1472 题目描述:在给定的 N 个整数中选出两个进行异或运算,求得到的结果最大是多少. 看到这 ...
- Python3 collections模块
https://www.cnblogs.com/zhangxinqi/p/7921941.html http://www.wjhsh.net/meng-wei-zhi-p-8259022.html h ...
- atcoder abc 244
atcoder abc 244 D - swap hats 给定两个 R,G,B 的排列 进行刚好 \(10^{18}\) 次操作,每一次选择两个交换 问最后能否相同 刚好 \(10^{18}\) 次 ...
- mybatis-plus分页插件
package com.tanhua.server.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterc ...
- Ubuntu安装python固定版本
一. 安装python3.7 本篇文章使用python3.7安装步骤为例 1.直接使用apt-get安装python3.7 apt-get install python3.7 该方法经常会出现unab ...