This article mainly introduces the DOM interactive operation technique of javascript performance optimization, and summarizes and analyzes various common optimization operation techniques of JavaScript in DOM operations based on examples, for more information, see the example in this article to describe the DOM interactive operation technique for javascript performance optimization. We will share this with you for your reference. The details are as follows:
In various aspects of javascript, DOM is undoubtedly the slowest part. DOM operations and interactions take a lot of time, because they often need to re-render the entire page or a part. Understanding how to optimize the interaction with DOM can greatly improve the speed of script completion.
1. Minimize DOM update
Take the following example:
Var list = document. getElementById ("ul"); for (var I = 0; I <10; I ++) {var item = document. createELement ("li"); item. appendChild (document. createTextNode ("item" + I); list. appendChild (item) ;}// this Code adds 10 projects to the list. Each project is added with two DOM updates. A total of 20 DOM updates are required.
We can use document fragments to minimize DOM updates.
Var list = document. getElementById ("ul"); var fragment = document. createDocumentFragment (); for (var I = 0; I <10; I ++) {var item = document. createELement ("li"); item. appendChild (document. createTextNode ("item" + I); fragment. appendChild (item);} list. appendChild (fragment );