jquery Performance Optimization

Source: Internet
Author: User

1. Updated version

2. Use the appropriate selector

$ (' #id ') best-of-breed positioning DOM element that can be searched at the target element's closest ID element

$ (' div ') second choice

$ ('. class ') more complex should be used selectively

$ (' [attribute=value] ') poor performance impact, avoid use

$ (': Hidden ') poor performance impact try not to use can be found at the parent or filter $ (' #content '). Find (': Hidden ')

$ (' A.button '). Filter (': Hidden ')

3. Cache objects

var $xxx =$ (' #xxx aaa.bbb ')

4, the loop to the DOM operation, should pay attention to as little as possible DOM operation.

5. When using jquery objects in arrays, it is recommended to use the for and while loops instead of $.each (), which can be faster code runs.

6, the event agent the following three kinds of code to the second and third first

  
    First type $ (' #myTable TD '). Click (function () {$ (this). CSS (' background ', ' red ');}) The second type of $ (' #myTable '). Click (function (e) {var $cilcked =$ (e.target); $cilcked. css (' background ', ' red ');}) The third type of $ (' #myTable '). On (' Click ', ' TD ', function () {$ (this). CSS (' background ', ' red ');})

7. Translate the duplicate code into a plugin

8. Use join () to stitch the string

9, try to use the native JS method

jquery Performance Optimization

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.