Tips and tricks are always useful as they save time and useful as well. So, today's post is collection of some random, short and quick jQuery tips. These tips are quite useful and can drastically improve performance of your webpage if you are not following them.
There are many ways to select element using selectors but that doesn't mean that all are equal. Always try to use ID and Element as selector as they are very fast. Even the class selectors are slower than ID selector.
When IDs are used as selector then jQuery internally makes a call to getElementById() method of Java script which directly maps to the element. When Classes are used as selector then jQuery has to do DOM traversal. So when DOM traversal is performed via jQuery takes more time to select elements.
Always use the compressed version of jQuery for production or final release as its size is 5 times smaller than Uncompressed version. jQuery library comes in 2 version Compressed and Uncompressed.
1. Production (Compressed Version)
2. Development (Uncompressed Version)
For development purpose, you can choose the development version of .js file as if you want to make some changes then that can be easily done. But ensure that when your software or product goes on production, always use the production version of .js file as its size is 5 times lesser than the development version. This can save some amount of bandwidth.
In software engineering, Don't Repeat Yourself (DRY) is a principle of software development aimed at reducing repetition of information. Duplication (inadvertent or purposeful duplication) can lead to maintenance nightmares, poor factoring, and performance issues. Just for the sake of completing the task, don't repeat your code as it increases the lines of code and reduces performance.
Use the latest version of jQuery as your website will be benefited from performance improvement done by the jQuery team in latest release. But Be SMART while referencing as you need to make sure it doesn't break existing code. Learn "How to always reference latest version of jQuery".
Don't use jQuery unless it is not necessary. If things can be achieved without jQuery then don't use it. There are so many things which can be achieved using CSS but people use jQuery for them. If you are as well, then think twice.
Feel free to contact me for any help related to jQuery, I will gladly help you.
Be Smart while using selectors
There are many ways to select element using selectors but that doesn't mean that all are equal. Always try to use ID and Element as selector as they are very fast. Even the class selectors are slower than ID selector.
When IDs are used as selector then jQuery internally makes a call to getElementById() method of Java script which directly maps to the element. When Classes are used as selector then jQuery has to do DOM traversal. So when DOM traversal is performed via jQuery takes more time to select elements.
Use Compressed version of jQuery
Always use the compressed version of jQuery for production or final release as its size is 5 times smaller than Uncompressed version. jQuery library comes in 2 version Compressed and Uncompressed.
1. Production (Compressed Version)
2. Development (Uncompressed Version)
For development purpose, you can choose the development version of .js file as if you want to make some changes then that can be easily done. But ensure that when your software or product goes on production, always use the production version of .js file as its size is 5 times lesser than the development version. This can save some amount of bandwidth.
Don't Repeat Yourself
In software engineering, Don't Repeat Yourself (DRY) is a principle of software development aimed at reducing repetition of information. Duplication (inadvertent or purposeful duplication) can lead to maintenance nightmares, poor factoring, and performance issues. Just for the sake of completing the task, don't repeat your code as it increases the lines of code and reduces performance.
Use Latest Version
Use the latest version of jQuery as your website will be benefited from performance improvement done by the jQuery team in latest release. But Be SMART while referencing as you need to make sure it doesn't break existing code. Learn "How to always reference latest version of jQuery".
Don't Use jQuery Blindly
Don't use jQuery unless it is not necessary. If things can be achieved without jQuery then don't use it. There are so many things which can be achieved using CSS but people use jQuery for them. If you are as well, then think twice.
Feel free to contact me for any help related to jQuery, I will gladly help you.
No comments:
Post a Comment