Jquery Performance Optimization Method

Source: Internet
Author: User

I have been searching

========================================================== ======================================
I. selector performance optimization suggestions
========================================================== ======================================

1.
This is

1 $ ('# content'). hide ();

Copy code

Or from

2 $ ('# content p'). hide ();

Copy code

2.
JQuery

3 varreceiveNewsletter =ter ('# nslForm input. on ');

Copy code

In the jQuery browser, it will traverse all

4 varcontent = $ ('div # content ');//

Copy code

Use

5 vartraffic_light = $ ('# content # traffic_light ');//

Copy code

3.
Cache the parent object for future use

6 varheader = $ ('# header ');

7 varmenu = header. find ('. menu ');

8 //

9 varmenu = $ ('. menu', header );

Copy code

4.
Self-version
Var linkContacts = $ ('. contact-links div. side-wrapper ');
Do not use
Var linkContacts = $ ('A. contact-links. side-wrapper ');

5.
. Find ()

10 vardivs = $ ('. testdiv', '# pageBody'); // 2353 on Firebug 3.6

11 vardivs = $ ('# pageBody'). find ('. testdiv'); // 2324 on Firebug 3.6-The best time

12 vardivs = $ ('# pageBody. testdiv'); // 2469 on Firebug 3.6

Copy code

6.
Use

13 $ ('Li. menu-item'). click (function () {alert ('test click ');})

14. css ('display', 'block ')

15. css ('color', 'red ')

16 fadeTo (2, 0.7 );

Copy code

Remember, never let the same selector appear multiple times in your code:
(

7.
If you often use selector in code, then the extension

17 $. extend ($. expr [':'], {

18 abovethefold: function (el ){

19 return $ (el). offset (). top <$ (window). scrollTop () + $ (window). height ();

20}

21 });

22 varnonVisibleElements = $ ('div: abovethefold ');//

Copy code

========================================================== ======================================
Ii. Optimization
========================================================== ======================================

8.
Cache frequently used elements:

23 varheader = $ ('# header ');

24 vardivs = header. find ('div ');

25 varforms = header. find ('form ');

Copy code

9.
The basic idea here is to create what you really want in the memory and then update
Direct

26 varmenu = '<ul id = "menu"> ';

27 for (vari = 1; I <100; I ++ ){

28 menu + = '<li>' + I + '</li> ';

29}

30 menu + = '</ul> ';

31 $ ('# header'). prepend (menu );

32 //

33 $ ('# header'). prepend (' <ul id = "menu"> </ul> ');

34 for (vari = 1; I <100; I ++ ){

35 $ ('# menu'). append ('<li>' + I + '</li> ');

36}

Copy code

Bubble event: unless in special circumstances

37 $ ("# entryform input"). bind ("focus", function (){

38 $ (this). addClass ("selected ");

39}). bind ("blur", function (){

40 $ (this). removeClass ("selected ");

41 });

Copy code

Of course, the above Code can help us complete the corresponding tasks, but if you want to find a more efficient method, please use the following code:

42 $ ("# entryform"). bind ("focus", function (e ){

43 var $ cell = (e.tar get); // e.tar get

44 $ cell. addClass ("selected ");

45}). bind ("blur", function (e ){

46 var $ cell = (e.tar get );

47 $ cell. removeClass ("selected ");

48 });

Copy code

You can perform operations on the target element by obtaining the focus and losing the focus in the parent listener. In the code above, the parent element plays the role of a dispatcher.

49 $ ('# myTable td'). click (function (){

50 rows (this).css ('background', 'red ');

51 });

52 improvement methods:

53 $ ('# mytable'). click (function (e ){

54 var $ clicked = require (e.tar get );

55 export clicked.css ('background', 'red ');

56 });

Copy code

Assume that

10.
Although

11.
For better performance, you should use direct functions such

12.
You often get

57 App. hiddenDivs =$ ('div. Den den ');

58 //

59 App. hiddenDivs. find ('span ');

Copy code

13.
Don't forget to use

60 $ ('# head'). data ('name', 'value ');

61 //

62 $ ('# head'). data ('name ');

Copy code

14.
Do not forget the simple and practical utility. What I like most is

15.
When
$ ('Html '). addClass ('js ');
Only when enabled
/*
. JS # myDiv {display: none ;}
So when


========================================================== ======================================
Iii. Suggestions on optimizing event Performance
========================================================== ======================================

16.
Sometimes

17.
When you have many nodes in a container, you want to bind an event to all nodes,

63 $ ("table"). delegate ("td", "hover", function (){

64 $ (this). toggleClass ("hover ");

65 });

Copy code

18.
If you want to compress

66/

67 $ (document). ready (function (){

68 //

69 });

70 //

71 $ (function (){

72 //

73 });

Copy code

========================================================== ======================================
Iv. Test
========================================================== ======================================

19. jQuery
Test Selenium, Funcunit, QUit, and QMock to test your code (especially plug-ins ). I want to discuss this topic in another topic because there are too many things to talk about.

20.
You often standardize your code to see which query is slow and then replace it. You can use jQuery to make the test easier:

74 //

75 $. l ($ ('div '));

76 //

77 $. time ();

78 //

79 $. lt ();

80 $ ('div ');

81 $. lt ();

82 //

83 $. bm ("var divs = $ ('. testdiv', '# pageBody');"); // 2353 on Firebug 3.6

Copy code

========================================================== ======================================
5. Other common
========================================================== ======================================

21.
The latest version is often the best. After changing the version, do not forget to test your code. Sometimes it is not completely backward compatible.

22.
New

23.
The best way to add styles to a few elements is to use

84 $ ('<style type = "text/css"> div. class {color: red ;}</style> ')

85. appendTo ('head ');

Copy code

24.
Set

25.
When you have determined which files should be loaded, package them into a file. You can use some open-source tools to automatically compress your files, such as using Minify (JSCompressor, YUI Compressor, or Dean Edwards JS packer. My favorite is JSCompressor.

26.
Use

27.
When your application is officially launched

86 //

87 <script type = "text/javascript" src = "[color = blue! Important] [url = Success "> </script>

Copy code

28 .(
Use

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.