Use fast DOM traversal with document.getElementById().
Given the availability of jQuery, it is much easier to produce highly specific selectors based on a combination of tag names, classes, and CSS3. You need to be aware that this approach involves several iterations while jQuery loops through each of DOM elements and tries to find a match. You can improve DOM traversal speeds by picking a specific element by ID.
The above code makes use of JQuery to manipulate the DOM which is not the best option as instead of doing it this way we can make use of the getElementById method which the document object provides us.
An alternative is to use defer in the script tag. The defer attribute specifies that the script should be executed after the page has finished parsing, but it only works for external scripts.
Use ‘switch’ instead of lengthy ‘if-then-else’ statements.
When your code base gets bigger a switch statement is usually more efficient than a set of nested ifs. This is because ‘switch’ statements can be optimized more easily during compilation.
Get rid of unnecessary loops and calls made inside loops.
Array push() pop() and shift() instructions have minimal processing overhead due to being language constructs closely related to their low-level assembly language counterparts.
Minimize your code as much as you can
Use the local scope (‘this’)
This is particularly useful for writing asynchronous code using callbacks, however, it also improves performance because you are not relying on global or closure variables held further up the scope chain. You can get the most out of the scope variable (this) by rewiring it using the special call() and apply() methods that are built into each function. See the example below:
- Performance of for vs foreach in PHP
- Difference Between Hadoop and SQL Performance
- HTML DOM Window performance Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.