JavaScript reduces reflux

Source: Internet
Author: User

Reduced reflux (reflows)

When the browser re-renders the elements in the document, they need to recalculate their position and geometry, which we call reflux. Reflux can block the user's actions in the browser, so it is very helpful to understand how to increase the reflow time.

Reflow Time Chart

You should trigger reflow or redraw in batches, but use these methods sparingly. It is also important to try not to handle the DOM. You can use DocumentFragment, a lightweight document object. You can use it as a way to extract part of the document tree, or create a new document "Fragment". Instead of adding DOM nodes continuously, use only one DOM insert after the document fragment to avoid excessive reflow.

For example, we write a function to add 20 div to an element. If you simply append a div into the element each time, this triggers 20 reflux.

function Adddivs (Element) {  var div;  for (var i = 0; i <; i + +) {    div = document.createelement (' div ');    div.innerhtml = ' heya! ';    Element.appendchild (div);}  }

To solve this problem, you can use DocumentFragment instead, we can add a new div to the inside each time. Adding DocumentFragment to the DOM after completion will only trigger a reflow.

function Adddivs (Element) {  var div;  Creates a new empty documentfragment.  var fragment = Document.createdocumentfragment ();  for (var i = 0; i <; i + +) {    div = document.createelement (' a ');    div.innerhtml = ' heya! ';    Fragment.appendchild (div);  }  Element.appendchild (fragment);}

JavaScript reduces reflux

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.