Improving performance re-using selected elements (Become an expert)

In this recipe, we'll see how we can store a collection of previously selected elements in a variable for a later processing and how this method can improve performance.

How to do it...

This time our goal will be a little different. Instead of applying margins or borders, we'll perform a comparison to show how in a (simulation of a) real-world page, caching objects can significantly improve performance. In order to understand the explanation, follow these steps:

  1. Create a copy of the template.html file and rename it as reusing-elements.html.
  2. Edit the <head> section of the page adding this code:
    <script> $(document).ready(function() { var start = new Date(); var $description = $('.description'); ...

Get Instant JQuery Selectors now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.