This article will share some best practices for efficient JavaScript to improve everyone’s understanding of the underlying and implementation principles of JS.
Data Storage
A classic problem in computer science is to obtain the best read and write performance by changing the location of data storage. In JavaScript, the location of data storage will have a significant impact on code performance. – If you can use {} to create an object, don’t use new Object. If you can use [] to create an array, don’t use new Array. The access speed of literals in JS is higher than that of objects. – The deeper a variable is in the scope chain, the longer it takes to access it. For this kind of variable, you can save it using local variables through caching to reduce the number of accesses to the scope chain - there is not much difference between using dot notation (object.name) and operator (object[name]), only Safari will There is a difference, click is always faster
Loop
Common loops in JS include the following types:
for(var i = 0; i < 10; i++) { // do something} for(var prop in object) { // for loop object} [1,2].forEach(function(value, index, array) { // 基于函数的循环})
There is no doubt that the first method is native, has the lowest performance consumption and is the fastest. The second method of for-in will generate more overhead (local variables) for each iteration, and its speed is only 1/7 of the first method. The third method obviously provides a more convenient loop method, but its speed Only 1/8 of the normal cycle. Therefore, you can choose the appropriate recycling method according to your project situation.
Event Delegate
Imagine adding an event to each A tag on the page. Will we add an onClick to each tag? When there are a large number of elements in the page that need to be bound to the same event handler, this situation may affect performance. Each event bound increases the load on the page or during runtime. For a rich front-end application, too many bindings will occupy too much memory on pages with heavy interaction. A simple and elegant way is event delegation. It is an event-based workflow: capture layer by layer, reach the target, and bubble layer by layer. Since there is a bubbling mechanism for events, we can handle events from all child elements by binding events to the outer layer.
document.getElementById('content').onclick = function(e) { e = e || window.event; var target = e.target || e.srcElement; //如果不是 A标签,我就退出 if(target.nodeNmae !=== 'A') { return } //打印A的链接地址 console.log(target.href) }
Redraw and rearrange
After the browser downloads HTMl, CSS, and JS, it will generate two trees: DOM tree and rendering tree. When the geometric properties of the Dom change, such as the width, height, color, and position of the Dom, the browser needs to recalculate the geometric properties of the element and rebuild the rendering tree. This process is called redrawing and rearrangement.
bodystyle = document.body.style; bodystyle.color = red; bodystyle.height = 1000px; bodystyke.width = 100%;
Modifying the three properties in the above method will cause the browser to reflow and redraw three times. In some cases, reducing this reflow can improve browser rendering performance. The recommended method is as follows, only perform one operation and complete three steps:
bodystyle = document.body.style; bodystyle.cssText 'color:red;height:1000px;width:100%';
JavaScript loading
IE8, Firefox3.5, and Chrome2 all allow JavaScript files to be downloaded. So