Javascript Performance Optimization-DOM Interactive Operation instance analysis-javascript skills

Source: Internet
Author: User
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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.