Right now I am using jQuery completely for my project (ajax, validations). But I guess due to my statements I assume that it is taking more time and how can I optimize the time for execution of each statement? Say for example I have seen some posts in Stackoverflow saying this statement:
$("div#mydialog").bind('Dialogclose',function(){});
is much slower than this:
$("#mydialog").bind('Dialogclose',function(){});
How can I attain this conclusion? Do I have S Tools for this? How can I optimise the statements in jQuery? What are the best practices to be used in jQuery?
each() jQuery's each() function is used to loop through each element of the target jQuery object — an object that contains one or more DOM elements, and exposes all jQuery functions. It's very useful for multi-element DOM manipulation, as well as iterating over arbitrary arrays and object properties.
To count how many times a function has been called, declare a count variable outside of the function, setting it to 0 . Inside of the body of the function reassign the variable incrementing it by 1 . The count variable will store the number of function invocations. Copied!
The jQUery delay () method sets a timer to delay the execution of the next item in the queue. Syntax: $(selector). delay (speed, queueName)
The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0.
Why not use the PROFILE jquery plug-in from plugins.jquery.com, which was made for just this purpose: PROFILE jquery plug-in
Try running your pages with the FireQuery FireFox extension.
http://firequery.binaryage.com/
FireQuery allows you to inject jQuery Lint (https://github.com/jamespadolsey/jQuery-Lint) into a page, giving you information on jQuery errors and incorrect usage.
You can also use a JavaScript profiler, such as FireBug in FireFox, the IE Developer Console in IE8 or higher, Developer Tools in Chrome, etc. This will give you execution times in each browser which can then be optimized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With