1) Try to avoid too many DOM manipulations
By avoiding DOM manipulations, you will greatly improve your code’s performance.
Examples:
Here’s the quick way:
Here’s the slow way:
This example demonstrates that the DOM is modified only once, instead of 50 times.
2) Better binding actions with functions
Here’s a code example:
Quick way:
Slow way:
The quick method is much better for performance, as the action is bonded to only one element instead of 3.
3) Use short form of declaration variables
Example:
Better way:
This will improve the readability of your code for you and other developers.
4) Use a ternary operator when it’s more appropriate
Example:
Better user ternary operator in this case:
Now you have 2 lines of code instead of 16.
5) Avoid deep "if" statements
Example:
Here is a better way of representing this function:
This method is much more readable and easier to understand.
6) Keep jQuery objects in variables
Example:
Better way:
This will save one DOM manipulation and improve the readability of your code
7) Don't overuse with variables
Example:
Better way:
See also:
Most Common Architecture Mistakes When Using MVC Paradigm