jQuery: Data Caching Tips to Improve Performance

In this section, I’ll cover different ways of storing data. Data caching can be really helpful in improving the performance of your web applications. So read carefully.

Different Ways of Storing Data

Don’t Use Attributes
Before the days of jQuery, a common practice was to store additional data in your DOM by adding custom attributes to your elements. You might see developers creating their own attributes or reusing less used attributes such as alt or title. This approach has several problems. If you create your own attributes, your code is not XHTML compliant, and if you reuse an attribute, you’re using one that really isn’t intended for that purpose. Here’s an example:

//Storing special information about the anchor in the alt attribute
$('a').attr('alt', 'Special info about this HTML tag');

//Getting the special information from the anchor's alt attribute
$('a').attr('alt')

Utilizing the jQuery data Function

A much better way to store information for a particular HTML element is to use the jQuery data function, shown here:

//Storing data in anchor tag
$('a').data('specialInfo', 'Special info about this HTML tag');
//Retrievind data from anchor tag
$('a').data('specialInfo');

Not only are you not reusing an attribute that wasn’t intended to hold additional information, but you are also able to name the key without jeopardizing XHTML compliance. Another great feature of the data function is that it doesn’t limit you to string data, which you are when you store data inside an attribute. You can store complex data structures inside the data function and then retrieve them again. You should note that by using this approach, you can’t store data directly in the HTML element at design time. Here’s an example of storing a complex data structure:

//Storing a complex data structure under the specialInfo key
$('a').data('specialInfo', {
firstName: 'Elijah',
lastName: 'Manor'
});
//Getting the complex data structure specialInfo & accessing the firstName property
$('a').data('specialInfo').firstName;

Actually, you can use a different syntax to store and retrieve information using the datamethod that is faster than using the key/value approach. Here’s an example:

$("li.demo").data({ firstName: "Elijah", lastName: "Manor" });

console.dir($("li.demo").data());

$("li.demo").data("someKey",
{ firstName: "Andrea", lastName: "Manor" });

console.dir($("li.demo").data());

console.log($("li.demo").data().someKey.firstName);

If you want, you can use both syntaxes at the same time. As the preceding example shows, you can attach an object literal directly to the data method and then add more data using the key/value syntax. Both pieces of information will be attached to the DOM element. When retrieved by using the data method, the result will look something like the following screen shot, which shows the output from the preceding code. Notice that you can reference the key you attached previously with a string and use that as a property name off of the result from data.

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

7 thoughts on “jQuery: Data Caching Tips to Improve Performance

  1. Hello,

    Your example isn’t really practical in my opinion.

    On a typical website we are typically looping and outputting data to the screen, where we might put data we need in the custom attribute.

    For example if I am showing 10 comments on the screen and I want to store the numeric id of the comment with the link so I can use it in JS then I could put it in a custom attribute field so it could be pulled via jquery.

    Using your example I would have to have on page JS do the looping to store it in the .data area.

    You are not the only one to provide an example like this.

    I have yet to see a practical example, though I haven’t spent too much time researching.

    What are your thoughts? Can you provide a better example?

    Randy

  2. I think you did not really understand the usage of .data function.
    If possible I will try to show its usage to you.
    Please wait.

  3. I was able to implement this feature on a photo gallery that I am working on. It grabs all the photos and photoids and stores the json on the body tag in a field called ‘picdata’.

    But the example doesn’t show how to use .data on the example in my earlier post.

    I am interested in your example in regards to my earlier post.

    Thanks!
    Randy

  4. Arvind07 — Have you done any testing on limitations for space, speed in accessing elements from the datastor vs on the data element itself? I’ve got about 10-20 MB of data on a given session and I’ve been exploring using $.data() but am trying to find the limitations! There must be something :p, if not for $.data() then at least on the browser JS side… I’ll try and run some tests but if you have any please let me know. It would make articles like this more valuable.

Comments are closed.