This time I will bring you a summary of Angular4Performance Optimizationmethods, what are the Notes for Angular4 performance optimization, the following is a practical case, let’s take a look.
Summary
Angular 4’s dirty value detection is an old topic, and understanding this model is the basis for Angular performance optimization. Therefore, today we will talk about the principle of dirty value detection in Angular 4 and look at some tips for performance optimization.
Entry Point - Zone.js
Angular 4 is an MVVM framework. After the data model (Model) is converted into a view model (ViewModel), it is bound to the view (View) and rendered into a page visible to the naked eye. Therefore, discovering the time point when the data model changes is the key to updating the page and calling dirty value detection.
After analysis, engineers found that data changes are often caused by asynchronous events such as macrotask and microtask. Therefore, by rewriting all asynchronous APIs in the browser, data changes can be effectively monitored from the source. Zone.js is such a monkey script (Monkey Patch). Angular 4 uses a customized Zone (NgZone), which notifies Angular that there may be data changes and needs to update the data in the view (dirty value detection).
Dirty value detection (Change Detection)
The basic principle of dirty value detection is to store the old value, and when detecting, compare the new value at the current moment with the old value Value comparison. If they are equal, there is no change. Otherwise, a change is detected and the view needs to be updated.
Angular 4 divides the page into several Components to form a component tree. After entering the dirty value detection, the detection is performed from top to bottom from the root component. Angular has two strategies: Default and OnPush. They are configured on the component and determine different behaviors during dirty value detection.
Default - The default strategy
ChangeDetectionStrategy.Default. It also means that this component is always tested once an event occurs that may change the data.
The operation of dirty value detection can basically be understood as the following steps. 1) Update the properties bound to the subcomponent, 2) Call the NgDoCheck and NgOnChangesLifecycle hooks of the subcomponent, 3) Update its own DOM, 4) Detect the dirty value of the subcomponent. This is a recursive equation starting from the root component.
// This is not Angular code function changeDetection(component) { updateProperties(component.children); component.children.forEach(child => { child.NgDoCheck(); child.NgOnChanges(); }; updateDom(component); component.children.forEach(child => changeDetection(child)); }
We developers will pay great attention to the order of DOM updates and the order of calling NgDoCheck and NgOnChanges. It can be found:
DOM updates are depth-first
NgDoCheck and NgOnChanges are not (nor are they depth-first)
OnPush - Single detection strategy
ChangeDetectionStrategy.OnPush. This component is only detected when the Input Properties change (OnPush). Therefore, when the Input does not change, it is only detected during initialization, also called a single detection. Its other behavior is consistent with Default.
It should be noted that OnPush only detects Input references. Input object's property changes will not trigger the dirty value detection of the current component.
Although the OnPush strategy improves performance, it is also a hot spot for bugs. The solution is often to convert Input into Immutable form and force the reference of Input to change.
Tips
Angular has 3 legal data binding methods , but their performance is different.
Bind data directly
- Name {{item.name}} Classes {{item.classes}}
In most cases, this is the best way to perform.
Bind a function call result
- Name {{item.name}} Classes {{classes(item)}}
In each dirty value detection process, the classes equation must be called once. Imagine that the user is scrolling the page, multiple macrotask are generated, and each macrotask performs at least one dirty value check. If there are no special needs, this method of use should be avoided as much as possible.
Bind data pipe
- Name {{item.name}} Classes {{item | classPipe}}
It is similar to the binding function. Every time the dirty value detection classPipe is called. However, Angular has optimized the pipe and added caching. If the item is equal to the last time, the result will be returned directly.
NgFor
多数情况下,NgFor应该伴随trackBy方程使用。否则,每次脏值检测过程中,NgFor会把列表里每一项都执行更新DOM操作。
@Component({ selector: 'my-app', template: `
- {{item.id}}
Reference
He who thinks change detection is depth-first and he who thinks it's breadth-first are both usually right
Angular Runtime Performance Guide
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Summary of Angular4 performance optimization methods. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
