Repository navigation
Expand file tree
/
Copy pathperformance.html
More file actions
855 lines (395 loc) · 44.4 KB
/
Copy pathperformance.html
File metadata and controls
855 lines (395 loc) · 44.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
<!DOCTYPE HTML>
<html lang="zh-hans" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>性能指引 · Regular官方指南</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<meta name="author" content="leeluolee">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="communication.html" />
<link rel="prev" href="mpregular.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="输入并搜索" />
</div>
<nav role="navigation">
<ul class="summary">
<li>
<a href="https://github.com/regularjs/regular" target="_blank" class="custom-link">Github</a>
</li>
<li class="divider"></li>
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
️介绍
</a>
<ul class="articles">
<li class="chapter " data-level="1.1.1" data-path="../intro/what.html">
<a href="../intro/what.html">
Regular是什么
</a>
</li>
<li class="chapter " data-level="1.1.2" data-path="../intro/hello.html">
<a href="../intro/hello.html">
️你好,Regular
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.2" data-path="../basic/">
<a href="../basic/">
基础
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="../basic/component/">
<a href="../basic/component/">
️组件
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="../basic/interpolation.html">
<a href="../basic/interpolation.html">
️插值
</a>
</li>
<li class="chapter " data-level="1.2.3" data-path="../basic/event.html">
<a href="../basic/event.html">
️事件
</a>
</li>
<li class="chapter " data-level="1.2.4" data-path="../basic/statement/">
<a href="../basic/statement/">
️语句
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.4.1" data-path="../basic/statement/list.html">
<a href="../basic/statement/list.html">
️列表渲染 - list
</a>
</li>
<li class="chapter " data-level="1.2.4.2" data-path="../basic/statement/if.html">
<a href="../basic/statement/if.html">
️条件渲染 - if
</a>
</li>
<li class="chapter " data-level="1.2.4.3" data-path="../basic/statement/include.html">
<a href="../basic/statement/include.html">
️结构引入 - include
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.2.5" data-path="../reference/expression.html">
<a href="../reference/expression.html">
表达式
</a>
</li>
<li class="chapter " data-level="1.2.6" data-path="../basic/data-binding.html">
<a href="../basic/data-binding.html">
数据监听
</a>
</li>
<li class="chapter " data-level="1.2.7" data-path="../basic/filter.html">
<a href="../basic/filter.html">
计算字段与过滤器
</a>
</li>
<li class="chapter " data-level="1.2.8" data-path="../basic/directive.html">
<a href="../basic/directive.html">
️指令
</a>
</li>
<li class="chapter " data-level="1.2.9" data-path="../basic/animation.html">
<a href="../basic/animation.html">
️动画
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" data-path="../advanced/">
<a href="../advanced/">
深入组件
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../advanced/modular.html">
<a href="../advanced/modular.html">
继承与模块化
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="../advanced/composite.html">
<a href="../advanced/composite.html">
️组合 - 结构复用
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="../advanced/modify.html">
<a href="../advanced/modify.html">
️动态注入
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="./">
<a href="./">
专题
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="template.html">
<a href="template.html">
模板管理
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1.1" >
<a target="_blank" href="https://github.com/regularjs/regular-loader">
单文件组件 - regular-loader
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.2" >
<a target="_blank" href="https://regularjs.github.io/regular-state/">
️单页系统
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="ssr.html">
<a href="ssr.html">
服务端渲染
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="mpregular.html">
<a href="mpregular.html">
微信小程序
</a>
</li>
<li class="chapter active" data-level="1.4.5" data-path="performance.html">
<a href="performance.html">
性能指引
</a>
</li>
<li class="chapter " data-level="1.4.6" data-path="communication.html">
<a href="communication.html">
️跨组件通信
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.6.1" data-path="redux.md">
<span>
与redux结合
</a>
</li>
<li class="chapter " data-level="1.4.6.2" data-path="rx.md">
<span>
与Rx结合(ing...)
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.7" data-path="todomvc.html">
<a href="todomvc.html">
50行代码的todomvc
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="../reference/">
<a href="../reference/">
索引手册
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="../reference/api.html">
<a href="../reference/api.html">
API
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="../reference/styleguide.html">
<a href="../reference/styleguide.html">
风格指南
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="../reference/directive.html">
<a href="../reference/directive.html">
✔内建指令
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="../faq/">
<a href="../faq/">
FAQ
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
本书使用 GitBook 发布
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >性能指引</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div class="search-plus" id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<div id="anchor-navigation-ex-navbar"><i class="fa fa-anchor"></i><ul><li><span class="title-icon "></span><a href="#regular-性能优化的几点建议"><b> </b>Regular 性能优化的几点建议</a></li><ul><li><span class="title-icon "></span><a href="#降低n-——-组件层级"><b>1. </b>降低N —— 组件层级</a></li><ul><li><span class="title-icon "></span><a href="#方案1-使用isolate-控制digest深度"><b>1.1. </b>方案1. 使用isolate 控制digest深度</a></li><li><span class="title-icon "></span><a href="#方案2-合理抽象组件"><b>1.2. </b>方案2. 合理抽象组件</a></li></ul><li><span class="title-icon "></span><a href="#降低m-平均监听者数量"><b>2. </b>降低M: 平均监听者数量</a></li><ul><li><span class="title-icon "></span><a href="#方案1-升级到v052版本以上"><b>2.1. </b>方案1. 升级到v0.5.2版本以上</a></li><li><span class="title-icon "></span><a href="#--k·p·t---"><b>2.2. </b> ` K·P·T` </a></li><li><span class="title-icon "></span><a href="#--k·p1·t--p2·t---"><b>2.3. </b> ` K·P1·T + P2·T` </a></li><li><span class="title-icon "></span><a href="#2-使用一次绑定表达式expression"><b>2.4. </b>2. 使用一次绑定表达式@(expression)</a></li></ul><li><span class="title-icon "></span><a href="#降低t--单个监听器的平均消耗时间"><b>3. </b>降低T: 单个监听器的平均消耗时间</a></li><ul><li><span class="title-icon "></span><a href="#1-尽可能带上list语句的by描述"><b>3.1. </b>1. 尽可能带上list语句的by描述</a></li><li><span class="title-icon "></span><a href="#2-升级到v052减少销毁时间"><b>3.2. </b>2. 升级到v0.5.2减少销毁时间</a></li></ul><li><span class="title-icon "></span><a href="#总结"><b>4. </b>总结</a></li></ul></ul></div><a href="#regular-性能优化的几点建议" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="regular-性能优化的几点建议"><a name="regular-性能优化的几点建议" class="anchor-navigation-ex-anchor" href="#regular-性能优化的几点建议"><i class="fa fa-link" aria-hidden="true"></i></a> Regular 性能优化的几点建议</h1>
<p>本文旨在用 20% 的精力解决使用Regular过程中 80% 的性能问题. </p>
<p>这里大部分是关于脏检查的性能优化,不了解的可以先看下<a href="../basic/data-binding.html#dirty">《Regular脏检查介绍》</a></p>
<p>首先,我们可以用一个简化后的公式来描述Regular的单次脏检查的复杂度</p>
<h3 id="--n·logn-·-m-·-t---"><a name="--n·logn-·-m-·-t---" class="anchor-navigation-ex-anchor" href="#--n·logn-·-m-·-t---"><i class="fa fa-link" aria-hidden="true"></i></a><code align="center"> N·logN · M · T </code></h3>
<p>其中</p>
<ul>
<li><strong>N</strong> : 代表组件深度</li>
<li><strong>M</strong> : 代表组件平均监听器数量</li>
<li><strong>T</strong> : 代表单个Watcher的检查时间 </li>
</ul>
<p>这样问题就落在了如何降低这三个因子了</p>
<h2 id="降低n-——-组件层级"><a name="降低n-——-组件层级" class="anchor-navigation-ex-anchor" href="#降低n-——-组件层级"><i class="fa fa-link" aria-hidden="true"></i></a>1. 降低N —— 组件层级</h2>
<p>这层是收益最高的方案,因为影响因子是 N·logN.</p>
<p><img src="https://p1.music.126.net/1jKdy2gjgJ_-AGih_OJPIQ==/109951163211165405.png" alt=""></p>
<p>以上图为例,叶子节点进行<code>$update()</code>时,会首先找到<code>DigestRoot</code> (默认情况下,即顶层使用 <code>new</code> 创建的组件),再层层向下进行组件的<code>$digest()</code>检查,在目前组件抽象较细致的开发习惯下,很容易产生10多层的组件深度,适当控制下digest深度可以得到可观的性能提升。</p>
<p><em>注 : 这个digest flow设计是为了避免产生网状更新链</em></p>
<h3 id="方案1-使用isolate-控制digest深度"><a name="方案1-使用isolate-控制digest深度" class="anchor-navigation-ex-anchor" href="#方案1-使用isolate-控制digest深度"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. 方案1. 使用isolate 控制digest深度</h3>
<p>第一个方式即使用<code>isolate</code>属性控制组件的数据流向,如<code><b isoalte /></code> 这样,在第一次初始化后,b组件就不再与a组件有任何数据绑定关系
<img src="https://p1.music.126.net/eooSITVC5hT4s47XqGGl1g==/109951163211169623.png" alt=""></p>
<p>如图所示,<code>b</code>组件此时就会成为<code>g</code>组件的实际<code>DigestRoot</code>。<code>b</code>组件内部的<code>$update</code>不会再会冒泡到外层</p>
<p>但这种方式同时让a的数据变更无法传达到b组件极其内部,如下图所示
<img src="https://p1.music.126.net/Q7rxGKxnvSDEAg_TIvR5vA==/109951163211175332.png" alt="enter image description here"> </p>
<p>如果需要实现<code>a->b</code>的单向传导,可以设置<code>isolate=1</code>
<img src="https://p1.music.126.net/XrXdN3qrNOY6oERisrJjLQ==/109951163211192124.png" alt="enter image description here"> </p>
<blockquote>
<p>isolate = 1 实际就形成了组件的单向数据流</p>
</blockquote>
<h3 id="方案2-合理抽象组件"><a name="方案2-合理抽象组件" class="anchor-navigation-ex-anchor" href="#方案2-合理抽象组件"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. 方案2. 合理抽象组件</h3>
<p>除了通过<code>isolate</code>手动控制更新树的深度之外,我们直接减小组件深度当然也可以。 但这似乎与React等框架推崇的方式相悖,其实不然。</p>
<p>过度抽象的组件,除了引入使用负担和增加组件层级外,无法带来直观的收益。 抽象记得要基于复用的前提,没有复用前提的组件抽象,除了让你的文件夹变得更复杂外,毫无益处。<em> 当然它可以给你带来好看的组件结构图 :)</em></p>
<h2 id="降低m-平均监听者数量"><a name="降低m-平均监听者数量" class="anchor-navigation-ex-anchor" href="#降低m-平均监听者数量"><i class="fa fa-link" aria-hidden="true"></i></a>2. 降低M: 平均监听者数量</h2>
<p>在<code>Dirty-Check Loop</code>中,在每个组件节点上都会经历<code>$digest</code>阶段: 遍历监听者数组,检查数据是否发生变更。</p>
<h3 id="方案1-升级到v052版本以上"><a name="方案1-升级到v052版本以上" class="anchor-navigation-ex-anchor" href="#方案1-升级到v052版本以上"><i class="fa fa-link" aria-hidden="true"></i></a>2.1. 方案1. 升级到v0.5.2版本以上</h3>
<p>首先将上面的公式再简化,并拓展到 <strong>一轮完整的脏检查<code>Dirty-Check Loop</code></strong> ,可以用下面的公式来表示</p>
<h3 id="--k·p·t---"><a name="--k·p·t---" class="anchor-navigation-ex-anchor" href="#--k·p·t---"><i class="fa fa-link" aria-hidden="true"></i></a>2.2. ` K·P·T` </h3>
<p>其中</p>
<ul>
<li>K: 脏检查稳定性检测轮数 (1~30次不等,30次仍不稳定将抛出错误)</li>
<li>P:digest影响到的所有监听器</li>
<li>T: 单个监听器的消耗时间</li>
</ul>
<p>在<a href="https://github.com/regularjs/regular/issues/129" target="_blank">Github: 0.5.2版本</a>,有一个优化就是讲监听器分为了 <strong>稳定监听器(stable)</strong> 和 <strong> 不稳定监听器(unstable)</strong></p>
<p>不稳定的监听器即具有<code>Side Effect</code>,比如</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">this</span>.$watch(<span class="hljs-string">'firstName'</span>, (firstName)=>{
<span class="hljs-keyword">this</span>.data.nickname = firstName + <span class="hljs-string">'先生'</span>
})
</code></pre>
<p>当<code>firstName</code>改变时,<code>nickname</code>也会随之改变,所以为了确保不出错,框架会检测多轮直到这类监听表达式不再变化</p>
<p>稳定的监听器就是一些没有<code>Side Effect</code>的监听比如大部分内置的监听(文本插值、r-html、属性插值等), 这类监听处理逻辑只有读操作,而没有写操作。其实只需要检测一次即可</p>
<p>这样公式就修改为了</p>
<h3 id="--k·p1·t--p2·t---"><a name="--k·p1·t--p2·t---" class="anchor-navigation-ex-anchor" href="#--k·p1·t--p2·t---"><i class="fa fa-link" aria-hidden="true"></i></a>2.3. ` K·P1·T + P2·T` </h3>
<p>其中 <strong><code>P1+P2 = P</code></strong> , P2 为Stable监听器, P1为非稳定。不要小看这个优化,由于内部监听器中, P2的比例很高(超过80%)所以在K>1的情况下,可以带来比较大的提升。</p>
<p>除此之外,你同时也可以自己主动来标记哪个监听器是属于<code>stable</code></p>
<pre><code class="lang-js">
<span class="hljs-keyword">this</span>.$watch(<span class="hljs-string">'title'</span>, (title)=>{
<span class="hljs-keyword">this</span>.$refs.top.innerText = title
}, {stable: <span class="hljs-literal">true</span>})
</code></pre>
<h3 id="2-使用一次绑定表达式expression"><a name="2-使用一次绑定表达式expression" class="anchor-navigation-ex-anchor" href="#2-使用一次绑定表达式expression"><i class="fa fa-link" aria-hidden="true"></i></a>2.4. 2. 使用一次绑定表达式@(expression)</h3>
<p>除非明确了不再对某个监听感兴趣,通过 <strong>一次绑定表达式</strong> 来提升性能其实并不是特别关键,但如果这个表达式正好在一个list循环中,那控制的收益会比较大,比如</p>
<pre><code class="lang-html">{#list list as item by item_index}
<span class="hljs-tag"><<span class="hljs-name">some-component</span> <span class="hljs-attr">list</span>=<span class="hljs-string">{@(item.list)}</span> /></span>
{/list}
</code></pre>
<p>如果这个列表有100项,那可以直接减少100个对<code>item.list</code>绑定(何况大部分情况都不止一个属性传入), 属于操作少收益大。</p>
<h2 id="降低t--单个监听器的平均消耗时间"><a name="降低t--单个监听器的平均消耗时间" class="anchor-navigation-ex-anchor" href="#降低t--单个监听器的平均消耗时间"><i class="fa fa-link" aria-hidden="true"></i></a>3. 降低T: 单个监听器的平均消耗时间</h2>
<p>其实每个表达式比如<code>user.firstName + '-' + user.lastName</code> 需要判断变化的开销各不相同,我们只需要针对高开销的监听器进行控制即可达到效果。</p>
<h3 id="1-尽可能带上list语句的by描述"><a name="1-尽可能带上list语句的by描述" class="anchor-navigation-ex-anchor" href="#1-尽可能带上list语句的by描述"><i class="fa fa-link" aria-hidden="true"></i></a>3.1. 1. 尽可能带上list语句的by描述</h3>
<p>list是最容易产生性能瓶颈的部分,下面做下简单说明</p>
<p>默认情况下,Regular使用的莱文斯坦编辑距离(Levenshtein Distance), 别被吓到了,实际上wiki百科等资源上都有完成的伪代码描述, 是个简单的常用算法。</p>
<p>它的优点是,不需额外标记,就可以找到尽可能少的步骤从一个字符串过渡到另一个(但并不保证相同值一定被保留), 数组同理. 这样映射到框架内部,就可以以尽可能少的步骤来变更DOM了,相信大家都知道DOM开销很大了。</p>
<p>但是它的时间复杂度是O(n^2) ,在大列表下会带来显著的性能开销, 甚至完全超过DOM更新的开销。</p>
<p>所以在Regular v0.3的某个版本引入了<code>by</code>的用法, 例如</p>
<pre><code class="lang-js">{#list items as item by item_index}
<li>{item.name}</li>
{/list}
</code></pre>
<p>顾名思义,新旧列表按顺序其<code>item_index</code>是不会变化的,即<code>0,1,2...</code> . 所以列表更新时,不会尝试去销毁重建,而是直接更新内部的值. 这种更新方式,内部的diff复杂度是 O(n), 属于极大的优化了性能.而且在DOM更新上比LS算法模式更轻量</p>
<p>这样用<code>by item_index</code>其实也带来一个问题,就是虽然循环对应的值改变了,但内部组件是不会重建的,即config、init不会被触发。</p>
<p>理论上 <code>by</code> 关键词之后可以接任意表达式,但是在之前版本是不生效的 (详情看<a href="https://github.com/regularjs/regular/issues/90" target="_blank">#90 regularJS的track by没起作用</a>) .</p>
<p>这个问题在最新版本已经被修复, 即你可以更精确的控制,是否要复用某一个项对应结构(内部组件是不会重建的,即config、init不会被触发)</p>
<pre><code class="lang-html">
{#list items as item by item.id}
<span class="hljs-tag"><<span class="hljs-name">li</span>></span>{item.name}<span class="hljs-tag"></<span class="hljs-name">li</span>></span>
{/list}
</code></pre>
<p>举个例子,只要<code>item.id ===0</code>的项还在,那对应的DOM结构就确保不会被回收,只会进行更新操作. 这里的时间复杂度也是O(n), 但实际开销会比<code>by item_index</code>高不少。</p>
<h3 id="2-升级到v052减少销毁时间"><a name="2-升级到v052减少销毁时间" class="anchor-navigation-ex-anchor" href="#2-升级到v052减少销毁时间"><i class="fa fa-link" aria-hidden="true"></i></a>3.2. 2. 升级到v0.5.2减少销毁时间</h3>
<p>在之前的版本, Regular的模板内容在销毁时,内部会进行大量的splice操作导致了性能问题,在0.5.2版本进行优化,整体销毁时间有了 <strong>数倍的提升</strong></p>
<h2 id="总结"><a name="总结" class="anchor-navigation-ex-anchor" href="#总结"><i class="fa fa-link" aria-hidden="true"></i></a>4. 总结</h2>
<p>从操作难易度和关键度上,主要是以下建议</p>
<ol>
<li>升级到Regular最新版本(也方便你使用最新的SSR、跨组件通信等特性),至少也是v0.5.2来整体提高性能(这个版本还做了不少别的性能优化)</li>
<li>list记得使用by语句,特别是<code>by item_index</code> (<code>item_index</code>取决于你的命名)</li>
<li>组件通过isolate来控制digest深度</li>
</ol>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="mpregular.html" class="navigation navigation-prev " aria-label="Previous page: 微信小程序">
<i class="fa fa-angle-left"></i>
</a>
<a href="communication.html" class="navigation navigation-next " aria-label="Next page: ️跨组件通信">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"性能指引","level":"1.4.5","depth":2,"next":{"title":"️\b跨组件通信","level":"1.4.6","depth":2,"path":"topic/communication.md","ref":"./topic/communication.md","articles":[{"title":"与redux结合","level":"1.4.6.1","depth":3,"path":"topic/redux.md","ref":"./topic/redux.md","articles":[]},{"title":"与Rx结合(ing...)","level":"1.4.6.2","depth":3,"path":"topic/rx.md","ref":"./topic/rx.md","articles":[]}]},"previous":{"title":"微信小程序","level":"1.4.4","depth":2,"path":"topic/mpregular.md","ref":"./topic/mpregular.md","articles":[]},"dir":"ltr"},"config":{"plugins":["edit-link","splitter","-lunr","-search","-sharing","search-plus","expandable-chapters-small","anchor-navigation-ex","force-http"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"splitter":{},"force-http":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":false,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"expandable-chapters-small":{},"edit-link":{"label":"编辑当前页","base":"https://github.com/regularjs/guide/edit/master/"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","author":"leeluolee","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Regular官方指南","language":"zh-hans","links":{"sidebar":{"Github":"https://github.com/regularjs/regular"}},"gitbook":"3.2.3","description":"Regular官方指南"},"file":{"path":"topic/performance.md","mtime":"2018-08-07T01:33:30.694Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-08-23T07:40:58.484Z"},"basePath":"..","book":{"language":""}});
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
<script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
<script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
<script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
<script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
<script src="../gitbook/gitbook-plugin-force-http/test.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>