In order to provide a fresh and unique user experience, many websites use JavaScript to improve design, validate forms, check browsers, Ajax requests, cookie operations, etc., to achieve dynamic effects without refreshing. However, a large amount of content needs to be rendered in the browser, and if it is not handled properly, the website performance will drop sharply. So we need to understand how to improve the execution efficiency of JavaScript.
JavaScript Function
In JavaScript, functions are precompiled before use. Although sometimes strings can be used instead of functions, this JavaScript code will be re-parsed every time it is executed, affecting performance.
1. eval example
2. setTimeout example
Using functions instead of strings as parameters ensures that the code in the new method can be optimized by the JavaScript compiler.
JavaScript scope
Each scope in the JavaScript scope chain contains several variables. It's important to understand scope chaining in order to take advantage of it.
function test() {
var localVar = "local"; //local variable
//Local variables
alert(localVar);
//Global variables
alert(this.localVar);
//If the document cannot be found in local variables, search for global variables
var pageName = document.getElementById("pageName");
}
Using local variables is much faster than using global variables because resolution is slower the further you go in the scope chain. The following figure shows the scope chain structure:
If there are with or try-catch statements in the code, the scope chain will be more complicated, as shown below:
JavaScript String
A function in JavaScript that greatly affects performance is string concatenation. Generally, symbols are used to splice strings. However, early browsers did not optimize this connection method, which resulted in the continuous creation and destruction of strings seriously reducing JavaScript execution efficiency.
It is recommended to change it to:
Let’s encapsulate it simply:
Then call it like this:
JavaScript DOM manipulation
HTML Document Object Model (DOM) defines a standard way to access and manipulate HTML documents. It represents an HTML document as a node tree containing elements, attributes, and text content. By using the HTML DOM, JavaScript can access all nodes in an HTML document and manipulate them.
DOM redraw
Every time the DOM object of the page is modified, it involves DOM redrawing, and the browser will re-render the page. Therefore, reducing the number of modifications of DOM objects can effectively improve the performance of JavaScript.
It is recommended to change it to:
' i '
');DOM access
Every node in the HTML document can be accessed through the DOM. Every time you call getElementById(), getElementsByTagName() and other methods, the node will be searched and accessed again. Therefore, caching the found DOM nodes can also improve the performance of JavaScript.
It is recommended to change it to:
DOM traversal
DOM traversal of child elements usually reads the next child element in a loop according to the index. In early browsers, this reading method has very low execution efficiency. The nextSibling method can be used to improve the efficiency of js traversing DOM.
It is recommended to change it to:
JavaScript memory release
In web applications, as the number of DOM objects increases, memory consumption will become larger and larger. Therefore, the reference to the object should be released in time so that the browser can reclaim the memory.
Release the memory occupied by DOM
Set the innerHTML of a DOM element to an empty string to release the memory occupied by its child elements.
Release javascript object