Book Image

Mastering jQuery Mobile

Book Image

Mastering jQuery Mobile

Overview of this book

Table of Contents (17 chapters)
Mastering jQuery Mobile
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

jQuery optimizations


We looked at various methods that we can implement to optimize images and CSS to improve the performance of web pages. In this section, we will take a look at some of the ways in which we can optimize our jQuery and JavaScript to enhance the performance of our web pages further. The first step to optimize JavaScript is to write clean and optimum JavaScript code. Assuming that we are writing the optimal JavaScript code for our web pages, let's take a look at some of the other things that can be done to optimize the code further.

Selector caching

You should always cache your jQuery selectors. It is extremely wasteful to constantly call $(selector) over and over with the same selector. If you are sure of using the same selector a number of times within your code, it makes complete sense to cache this selector. This improves the page speed considerably. Let's take a look at a simple code snippet to understand how you can cache your selectors:

var cache = $(this).parent().parent...