Front end performance optimization in 10 aspects
Reprinted articles:
Front end performance optimization in 10 aspectsAkik: wait for the wind to come to the blog garden
1. Loading optimization
- Compression consolidation
- Code Spliting, which can be based on routing or dynamic loading
- Third-party modules are placed in CDN
- When large modules are loaded asynchronously, such as Echarts, you can use require.ensure to display the corresponding chart after successful loading
- Small module moderate merge, some scattered small module merge together to load, faster
- You can use pefetch to preload, which is perfect for step-by-step scenarios
2. Image optimization
- Minimize using Sprite, IconFont, Base64 inline
- Images are lazy to load
- WebP replaces other formats
- The picture must be compressed
- The SRCSET of IMG can be used to display pictures of different sizes according to different resolutions, which not only ensures the display effect, but also saves bandwidth and improves loading speed
3. The CSS optimization
- CSS is written in the header
- Avoid CSS Expressions
- Remove empty CSS rules
- Avoid inline style styles
4. Js optimization
- Js is written at the bottom of the body
- JS uses Defer to put it in the head, preloading the load time without blocking the DOM parsing
- The script tag adds crossOrigin to facilitate error collection
5. Rendering optimization
Minimize reflow and repaint, which are triggered by style, size, node addition and subtraction operations.
1.2 Batch the DOM with a DocumentFragment or InnerHTML. 1.3 The DOM is hidden, or copied to memory, similar to the Virtual DOM, and modified. 1.4 Animation elements must be absolute, out of the document stream, without affecting other elements. Instead of animation with left, top, etc., use transform and opacity and turn on the render layer (will-change or translate3D (0,0,0)). Don’t use Timer 1.6 Mobile Hardware Accelerator to trigger GPU rendering, or Translate3D (0,0,0)
- Try to use CSS animation instead of JS animation and Canvas animation instead of JS animation
- For initial rendering, you can use skeleton screen or loading to enhance the experience
- PWA, can be local cache resources, improve the experience
- Frequently triggered events, anti-chattering, throttling, such as scroll, input, etc
- Long lists, use paging or scroll loading, virtual lists, remove off-screen DOM
6. First screen optimization
Principle: fast display, smooth scrolling, lazy loading, lazy execution, progressive display
- Separate code that is not needed on the first screen
- Server-side rendering or pre-rendering, direct rendering after loading HTML, reduce the white screen time
- DNS PREFETCH, the use of DNS-PREFETCH reduces DNS query time, PC domain divergence, mobile domain convergence
- Reduce critical path CSS. You can inline critical CSS, which reduces load and render time
7. Packing optimization
Mainly Webpack optimizations
- Unpacking externals dllPlugin
- Extract commonchunkPlugin or splitChunks from public packages
- Narrow the scope of various loader configurations include and exclude, noParse skipping files
- Enable cache of various loaders
- Multi-threading accelerates happypack or thead-loader
- Tree-shaking ES module analysis, remove dead code
- Scope Hoisting ES6 module analysis, combining multiple modules into one function, reduces the memory occupation, reduces the volume, and prompts the running speed
Webpack long cache optimization
8.1 JS files use chunkhash, not hash
8.2 CSS files use Contenthash, not chunkhash, and are not affected by JS changes
8.3 Vendor extraction, the public library is not affected by the change of business module
8.4 Inline Webpack Runtime to page, chunkId changes do not affect vendor
8.5 Ensure that the module ID is stable. Instead of using a number as the module ID, use the hash value of the file content instead. Use hashedModuleIdSplugin
8.6 Make chunkhash stable and use Webpack-chunk-hash instead of Webpack’s own hash algorithm. Webpack’s own hash algorithm will calculate different hash values for the same file under different development environments, which cannot meet cross-platform requirements.
8. Vue optimization
- Routing lazy load components
- Keep-Alive cache component to keep the original display state
- Add a key to the list item to ensure it is unique
- List items bind events, using the Event Proxy (V-For)
- V-IF and V-FOR should not be used on the same tag, it will perform V-IF judgment on each item
9. The react optimization
- Routing components are lazy to load using React – Loadable
- Class component to add shouldComponent or PureComponent
- Add Reaction. memo to function component
- Add a key to the list item to ensure it is unique
- Function components use hook optimization, useMemo, useCallback
10. SEO optimization
- Add various meta information
- pre-rendered
- Server-side rendering