Home Web Front-end HTML Tutorial [Front-end talk] Break down the details of front-end optimization_html/css_WEB-ITnose

[Front-end talk] Break down the details of front-end optimization_html/css_WEB-ITnose

Jun 24, 2016 pm 12:03 PM
optimization front end

I’m glad to see that more and more companies are paying attention to front-end development. Front-end is no longer a subsidiary skill of website developers. Thinking back to when I first started in the industry, ASP websites (non-ASP.NET) were popular at that time, and 80% of websites were made with ASP. A web page might be an ASP file, with front-end code and back-end code mixed together. It's different now. The front and back ends are separated. The front end can focus on display without paying too much attention to the implementation of the back end. However, because the front-end develops so fast and the display environment is complex and changeable, front-end developers need to master a wider range of knowledge.

In this miscellaneous talk, I plan to introduce some to you from a relatively broad perspective (including JS, CSS, network transmission, HTML5, etc.), taking the whole into parts as a starting point Front-end optimization methods. (Due to limited personal knowledge and experience, in addition to the points mentioned in the article, there must be other optimization methods that have the characteristics of turning zero into wholes . Everyone is welcome to add to it. This article will be updated from time to time)

What is turning zero into whole? In layman's terms, it means that we integrate the broken things into a whole. By operating this whole, we can also achieve the effect of operating the broken things one by one. For example, if you give me four apples, I can walk around with them in both hands; if you give me ten apples, and I carry them all in my hands, I may walk slower because I have to take care of the apples and am afraid of falling; but , if you give me another bag and I put ten apples in it, they will become a whole, and I can carry it everywhere.

OK, having said this, let’s start counting the places in the front end that need to be rounded up.

1. atlas/sprite assets/elf map/sprite map

These are the names for the image collection, which is what the image collection is doing When selecting image resources, concentrate some small image elements into one image. When used, some parameters are used to display the required image elements in the large image collection. For example, if an ICON of this large image is needed as the background of a DIV, in addition to setting the large image as the background of the DIV, you also need to change the position of the background image to display a specific small image.

Example: The sprite assets of a website are as follows:

2. Event delegation

Interview At that time, I often asked a question. There is a list page. The page structure is a BODY outside, with 10,000 sub-DIVs inside. Each DIV contains some text, and these texts are different. The requirement is that when a DIV is clicked, an alert box pops up to display the text in the DIV. what will you do.

30% of people will use JQuery to bind events, thinking that it looks simple and should be efficient, such as: $("body>div").click(...), and 20% of people will write Use native statements to traverse and add click events to each DIV. 10% of people answered that when the page scrolls, save the height of the page scroll. When clicked, get the Y coordinate of the trigger point on the screen and change the Y coordinate. Add it to the scroll height of the page, and then divide it by the height of each DIV to know which element you clicked, and then pop up the content of this element. (Or other similar methods)

Interviewers who answer the above answer will be deducted points for this question. The correct answer is that you only need to add a click event to the external element (this question is BODY). When clicking , you can get the source element that triggered the click event (such as a DIV), and pop up the text content of this element.

3. Long connection keep alive

This is related to the back-end, not just the front-end. Most web pages use http to obtain resources (such as pictures, style sheet files, JS files, etc.). Some servers will not maintain the connection. When a request comes, the connection will be established, and then the content will be output. After the output is completed, the connection will be disconnected. , and then re-establish the connection and output for the next request. If a web page needs to request a hundred files, the server will establish and disconnect a hundred connections. Each establishment and disconnection takes time (even longer than the time to transfer the data), which undoubtedly increases the rendering time of the page. If keep alive is enabled on the server, it will not only reduce the load on the server, but also speed up the page opening time.

As shown in the figure below, we can see that the actual time to receive content (Receiving) is very short, and most of the time is spent in waiting (Waiting) and blocking (Blocking).

4. Multi-DOM merging

As the front-end performance becomes richer and richer, there are more and more things on the page. The problem is that there are too many DOMs, which will cause performance problems when doing some DOM operations. When there are a lot of DOMs, it is necessary to merge the DOMs. You can consider merging multiple DOMs into a small number, or use CANVAS for display if possible.

For example: some requirements will require front-end developers to make this kind of date picker

If you use a DOM to install an option (such as: 1970), regardless of the button, if you look at the date option alone, you may need 44 12 31 = 87 DOM. For mobile browsers, the fewer DOM, the better . What if the product thinks about it later and adds hours, minutes and seconds options in addition to the date? This requires 44 12 31 24 60 60 = 231 DOMs. But we can think about it carefully. If each field (year, month, day) is only contained in a very tall container (such as DIV), the content can be wrapped with line breaks, and the value is obtained through some algorithms. Isn’t the number of DOM significantly reduced? From 87 DOMs to 3 DOMs. An order of magnitude less.

5. WebSocket

Many web pages have the requirement to update data in real time, such as securities websites. In order to avoid page refresh, AJAX will be used to perform long polling, and the data will be fetched from the server every once in a while (such as one second). This will cause a large number of request connections, which will not only put a lot of pressure on the front end, but also on the back end. Fortunately, HTML5 has brought us WebSocket. The web page can maintain a long connection with the server and maintain real-time updates of data through the long connection.

6. CSS attribute inheritance

CSS will inherit the attributes of the parent element. We can add some common attributes to the CSS of the parent element. Definition, child elements obtain these elements through inheritance. In some cases, proper use of inheritance can significantly reduce the size of CSS files.

7.documentFragment

"A list page requires you to insert a thousand DOMs into it. How do you do it?" This is me A question I often ask others.

Many times, when you add a DOM to a page, it will cause the page to be reconstructed. If you use traversal to add them one by one, the page will be re-rendered every time, which will cause performance degradation. A good way is to cache the elements to be added first and add them all at once. Generally, documentFragment is used for caching, and some use long strings (HTML code strings) for caching.

8. Merge multiple animations into one

When doing CSS3 animation, if the requirements require multi-step animation (such as, Rotate a DIV 30 degrees along the Y axis, then rotate 30 degrees along the X axis, and then rotate 30 degrees along the Z axis). We can merge these animations and use animation keyframes to isolate these steps. Save the cost of multi-step DOM operations.

9. Multi-file compression and merging

Compressing JS and CSS files is an effective way for the front-end to reduce network transmission volume (compressed The general method is to remove extra spaces and newlines, and replace variable names or method names. YUI Compressor is a good tool)

Merge multiple small JS or CSS files into one large file. This approach can significantly reduce the number of requests for instructions.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Vue: a perfect pairing of front-end development tools PHP and Vue: a perfect pairing of front-end development tools Mar 16, 2024 pm 12:09 PM

PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

C++ program optimization: time complexity reduction techniques C++ program optimization: time complexity reduction techniques Jun 01, 2024 am 11:19 AM

Time complexity measures the execution time of an algorithm relative to the size of the input. Tips for reducing the time complexity of C++ programs include: choosing appropriate containers (such as vector, list) to optimize data storage and management. Utilize efficient algorithms such as quick sort to reduce computation time. Eliminate multiple operations to reduce double counting. Use conditional branches to avoid unnecessary calculations. Optimize linear search by using faster algorithms such as binary search.

Exploring Go language front-end technology: a new vision for front-end development Exploring Go language front-end technology: a new vision for front-end development Mar 28, 2024 pm 01:06 PM

As a fast and efficient programming language, Go language is widely popular in the field of back-end development. However, few people associate Go language with front-end development. In fact, using Go language for front-end development can not only improve efficiency, but also bring new horizons to developers. This article will explore the possibility of using the Go language for front-end development and provide specific code examples to help readers better understand this area. In traditional front-end development, JavaScript, HTML, and CSS are often used to build user interfaces

Combination of Golang and front-end technology: explore how Golang plays a role in the front-end field Combination of Golang and front-end technology: explore how Golang plays a role in the front-end field Mar 19, 2024 pm 06:15 PM

Combination of Golang and front-end technology: To explore how Golang plays a role in the front-end field, specific code examples are needed. With the rapid development of the Internet and mobile applications, front-end technology has become increasingly important. In this field, Golang, as a powerful back-end programming language, can also play an important role. This article will explore how Golang is combined with front-end technology and demonstrate its potential in the front-end field through specific code examples. The role of Golang in the front-end field is as an efficient, concise and easy-to-learn

How to optimize the startup items of WIN7 system How to optimize the startup items of WIN7 system Mar 26, 2024 pm 06:20 PM

1. Press the key combination (win key + R) on the desktop to open the run window, then enter [regedit] and press Enter to confirm. 2. After opening the Registry Editor, we click to expand [HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorer], and then see if there is a Serialize item in the directory. If not, we can right-click Explorer, create a new item, and name it Serialize. 3. Then click Serialize, then right-click the blank space in the right pane, create a new DWORD (32) bit value, and name it Star

Vivox100s parameter configuration revealed: How to optimize processor performance? Vivox100s parameter configuration revealed: How to optimize processor performance? Mar 24, 2024 am 10:27 AM

Vivox100s parameter configuration revealed: How to optimize processor performance? In today's era of rapid technological development, smartphones have become an indispensable part of our daily lives. As an important part of a smartphone, the performance optimization of the processor is directly related to the user experience of the mobile phone. As a high-profile smartphone, Vivox100s's parameter configuration has attracted much attention, especially the optimization of processor performance has attracted much attention from users. As the "brain" of the mobile phone, the processor directly affects the running speed of the mobile phone.

Analysis of front-end engineer responsibilities: What is the main job? Analysis of front-end engineer responsibilities: What is the main job? Mar 25, 2024 pm 05:09 PM

Analysis of front-end engineer responsibilities: What is the main job? With the rapid development of the Internet, front-end engineers play a very important professional role, playing a vital role as a bridge between users and website applications. So, what do front-end engineers mainly do? This article will analyze the responsibilities of front-end engineers, let us find out. 1. Basic responsibilities of front-end engineers Website development and maintenance: Front-end engineers are responsible for the front-end development of the website, including writing the website’s HTML, CSS and JavaScr

See all articles