On the mobile side, because of the phone configuration and 3/4g network reasons, from two aspects to solve the performance optimization problem, 1. Load not more than 3 seconds, with loading or resources do not exceed 1M. 2. Rendering speed.
Based on the above two aspects, all code that affects the first screen loading and rendering should be placed in the processing logic.
Load Optimization:
1.Http Request:
To reduce the HTTP request, the phone browser responds to requests at the same time as 4 requests, so combine JS and CSS as much as possible and merge the images.
2. Cache:
Using caching can reduce the number of requests to the server and save load time, so all static resources are set up on the server side, and as far as possible using long cache (the update of long cache resources can use timestamp)
3. Compress HTML, CSS, JavaScript:
Reducing the size of your resources can speed up the display of your Web pages, so code compression for HTML, CSS, JavaScript, and so on, and set up gzip on the server side.
4. No blocking:
JavaScript written on the HTML header (no async), and the style written in the HTML tag block the rendering of the page, so the CSS is placed on the page header and introduced using link, to avoid writing style in HTML tags, JavaScript is placed at the end of the page or loaded asynchronously.
5. Load on Demand:
The resources that do not affect the first screen and the resources used by the current screen resources are loaded when the user needs them, which can greatly improve the display speed of important resources and reduce the overall traffic.
However, it is important to note that loading on demand causes a lot of repainting, affecting rendering performance, and the solution is:
(1) lazyload.js (see blog about lazyload.js)
(2) Rolling screen loading
(3) Loading via media query
6. Pre-load:
A large heavy resource page, such as a game, can be used to add loading, and the resource is loaded and the page is displayed. But the loading time is too long, will cause the user to churn.
For user behavior analysis, you can load the next page of resources on the current page and increase the speed.
(1) Perceptible loading (such as loading entering a space game)
(2) Non-perceptible loading (such as loading the next page in advance)
7. Compress the Image:
The picture is the most traffic resource, so try to avoid using him, when using the most appropriate format (to achieve the premise of the requirements, size judgment), the appropriate size, and then use the smart image compression, and in the code with Srcset to display on demand.
Note: Excessive compression of picture size affects picture display effects
(1) Using smart maps (http://zhitu.tencent.com/)
(2) Use other means instead of pictures (1. Use CSS3 2. Using SVG 3. Using Iconfont)
(3) using Srcset
(4) Select the appropriate picture (1. WEBP is better than JPG 2. PNG8 better than GIF)
(5) Select the appropriate size (1. The first load is no greater than 1014KB 2. Not wider than 640 (based on the general width of the phone screen))
8. Reduce cookies:
Cookies affect loading speed, so static resource domain names do not use cookies.
9. Avoid redirects:
Redirection affects the load speed, so the server is set up correctly to avoid redirection.
10. Load third-party resources asynchronously:
Non-control of third-party resources affects page loading and display, so third-party resources are loaded asynchronously.
script Execution Optimizations:
Improper script handling can block page loading and rendering, so be aware of it when you use it:
CSS is written in the head, JavaScript is written at the tail or asynchronously.
Avoid empty src, such as pictures and iframe, and empty SRC will reload the current page, affecting speed and efficiency.
Try to avoid resetting the image size.
Resetting a picture size means resetting the size of a picture multiple times in a page, CSS, JavaScript, and so on, resetting the size of the picture multiple times can cause multiple redraw of the picture, affecting performance.
Picture try to avoid using Dataurl,dataurl image compression algorithm files that do not use images will become larger, and will be decoded before rendering, loading slow time-consuming.
CSS Optimizations:
Try to avoid writing the style attribute in the HTML tag
1. Avoid CSS expressions:
The execution of the CSS expression needs to be rendered outside the CSS tree, so avoid CSS expressions.
2. Remove the empty CSS rule:
Empty CSS rules increase the size of the CSS file and affect the execution of the CSS tree, so you need to remove the empty CSS rules.
3. Use the display's properties correctly:
The display property affects the rendering of the page, so please use it properly.
(1) Width, height, margin, padding, and float should not be used after display:inline
(2) Float should not be used after display:inline-block
(3) Vertical-align should not be used after display:block
(4) You should not use margin or float after display:table-*
4. No misuse of float:
Float is computationally large at render time and minimizes usage.
5. Do not misuse Web fonts:
Web fonts need to download, parse, redraw the current page and minimize use.
6. Do not declare too many font-size:
Too much font-size raises the efficiency of the CSS tree.
7. A value of 0 does not require any units:
For browser compatibility and performance, do not bring units with a value of 0.
8. Standardize various browser prefixes:
(1) No prefix should be placed in the last
(2) CSS animation can only be used (-webkit-without prefix) two kinds of
(3) Other prefixes are-webkit--moz--ms-prefix four, (-o-opera browser instead of blink kernel, so obsolete)
9. Avoid making selectors look like regular expressions:
Advanced selector execution takes a long time and is difficult to read and avoids use.
JavaScript execution Optimizations:
1. Reduce repainting and reflow:
(1) Avoid unnecessary DOM operations
(2) Try to change class instead of style and use classlist instead of classname
(3) Avoid using document.write
(4) Reduce drawimage
2. Cache Dom Selection and calculation:
Each time the DOM selection is calculated, cache him.
3. Cache list. Length:
Every. Length is calculated, and a variable is used to save the value
4. Use event proxies as much as possible to avoid bulk binding events:
Use the ID selector as much as possible, and the ID selector is the quickest.
5.TOUCH Event Optimization:
Use Touchstart, Touchend instead of click, because fast impact speed. However, it should be noted that touch response is too fast and easy to cause misoperation
Rendering Optimizations:
1.HTML using viewport
Viewport can speed up the rendering of the page, use the following code:
<namecontentinitial-scale=1″>
2. Reduce the DOM node:
DOM node too much affects the rendering of the page, should try to reduce the DOM node
3. Animation optimization:
(1) Use CSS3 animations as much as possible
(2) Rational use of requestanimationframe animation instead of settimeout
(3) Use canvas animations appropriately using CSS animations within 5 elements, more than 5 using canvas animations (iOS8 can use WebGL)
4. High-frequency event optimization:
Touchmove, Scroll events can cause multiple renderings
(1) Use Requestanimationframe to monitor frame changes to make rendering at the right time
(2) Increase the time interval for response changes and reduce redraw times
5.GPU Acceleration:
The following properties in CSS (CSS3 transitions, CSS3 3D transforms, Opacity, Canvas, WebGL, Video) to trigger GPU rendering, please use it properly.
Note: The use of the transition will cause the phone to consume more power.
This article turns from friend Http://www.cnblogs.com/xinxingyu/p/4753239.html
H5 Mobile front-end performance optimization