Preface Why optimizing performance is so important for front-end engineers
There is a saying in the industry that I don’t know if you have heard of it, ‘The performance consumption of code written by people who understand performance optimization and have studied jquery source code will be hundreds of times different from those who don’t understand performance optimization. Even thousands of times ', the current javascript is a transition process from ECMAscript3 to ECMAscript5 and ECMAscript6. The problems caused by improper coding methods when JavaScript is not perfect cannot be ignored.
Performance optimization
Now I will share with you some of my insights on performance optimization;
1. Sprite map
The most basic thing is to make the background image into a sprite map as much as possible to reduce image requests, so another basic instinct of general web engineers is the production of sprite maps .
2.css selector optimization
In CSS, try to use descendant selectors as much as possible>, and use descendant selectors less. When using descendant selectors, the search engine will search all descendant elements. If we use descendant selectors, , you can narrow the scope of the search, thereby reducing the performance consumption of the search engine.
3.js changes the style and directly operates the class name
When operating element styles in js, do not use style to directly add styles. Generally, the performance will not be affected much if there are few attributes. In fact, this is not the case. Every time a style is added, the page will be redrawn, and redrawing must be paid attention to. , when operating styles, directly operating the class name will only cause one redraw, while directly adding styles with style will cause multiple redraws.
4.js directly operates dom nodes
When operating a node, try to add the node after the element. If you insert it in front of the node, the nodes after the inserted node will cause reflow. When inserting to the back, you only need to reflow the inserted node once. .
Some people may not understand the concepts of redrawing and reflow
5. Regular matching selector
Attribute selectors in css3 and jQuery. Some of these selectors use regular matching. Try not to use them. Of course, if performance optimization is not considered, these methods are still easier to use. Yes, the regular matching selector will cause the search engine to search all tags, which will greatly affect performance
6.js element acquisition optimization
When getting elements in js, you normally use document.getElementsById. The search engine will search from the bottom of the Dom tree until it finds the document in the window and then returns to the search, so It is best to store the document.body when obtaining the element. When using it again, just take out this variable and use it, which can save the performance of the search engine
7. Memory overflow
Generally, when running recursion, memory overflow will occur, causing a significant drop in performance when running recursion. After the operation is completed, the memory will be recycled by the system, so when running recursion, an object needs to be used to save the value. It is detected during operation. If it exists, it will be returned directly. If it does not exist, it will be added. This can solve the great performance of recursion.
8. Use GET request for Ajax
POST request is implemented by sending HTTP request header first and then sending data. GET has no request header, but it should be noted : GET size limit is about 4K, while POST has no limit.
9. Lazy loading of images
because the request volume is too large can cause the image to be loaded lazily. When the page scrolls to the position of the image, the image will be loaded again.
Now I recommend a plugin for lazy loading of images
jquery.lazyload.js
10.避免图片src属性为空
src 属性是空字符串的图片很常见,主要以两种形式出现:
var img = new Image();img.src = “”;
这两种形式都会引起相同的问题:浏览器会向服务器发送另一个请求。
技巧
1.排他思想
先排除当前所有的,再进行下一次操作;一般在进行动画、添加样式等时,在二次操作时先清楚前面的样式再从新添加新的样式,避免同时进行动画的冲突。
2.短路运算(||)
一般在一个值需要判断是否存在时,尽量不要使用if语句,可以使用短路运算符,可以省去代码占用的内存。比如:
a=a||b;
a存在则使用a,a不存在则使用b。
3.运算(&&)
可以运用在条件匹配时,以便条件的多余查询,比如判断一个对象是否为数组时,
a && a.length&& a.length>=0
4.伪数组以及数组
当需要封装一个非数组元素变为一个数组时,最简单的办法就是给其添加一个[],如果需要是一个伪数组时,可以给其设置一个length属性。
5.节流阀
一般运用在动画中,设定一个值,开始时设置为true,判断其值,在进入动画时给这个值赋值为false,在动画结束时使用回调函数再重新赋值为true。
6.解除文本的被动选中状态(纯干货)
当在点击一些按钮时,有时会出现文本被选中的情况,让客户看见就犹如bug一般,以下是解除这一状态的代码,粘贴即可。
if(document.all){ document.onselectstart= function(){return false}; //for ie }else{ document.onmousedown= function(){return false}; document.onmouseup= function(){return true}; } document.onselectstart = new Function('event.returnValue=false');
7.巧妙使用三元运算符
当需要判断一个值是否存在,也可以使用三元运算符使代码更加优化。比如
obj=obj===undefined?Object:obj;
补充:
上述是自己在工作中,总结的前端优化以及一些技巧与大家分享,如果大家有什么好的优化以及技巧希望可以多多评论,个人认为技术是需要多沟通才可以更好的进步。