1

I've recently looked at some front end js code.

(1) In some cases the code creates a new JavaScript object from a JSON received from the back end. Then the newly created object is stored in a cache (say a map). This way the object would be cached on the front end for later use.

(2) I've also seen instances when the new JavaScript object (called eventObject here) is stored in the DOM like this: $(this).data('eventObject', eventObject);

Does it matter which way you store the data? I would personally cache eventObject in a JavaScript cache object (ie make your own cache class or a map). Isn't it simpler to cache like this than mess with the DOM and then you have to remember where you put what?

In my searches I've looked at XML DOM vs Object in Javascript

2
  • Your #2 method is jQuery, not vanilla JavaScript. Under the hood, I believe it is stored in a cache object of sorts (you can look at the data() method in the jQuery source for more details). Commented Dec 23, 2012 at 20:20
  • 1
    Using jQuery data() at runtime will actually store all data in $.cache, so there's no real difference. You only gain another way of referencing your data. Commented Dec 23, 2012 at 20:21

1 Answer 1

1

Isn't it simpler to cache like this than mess with the DOM and then you have to remember where you put what?

jQuery's data method actually doesn't mess with the DOM, it is just a convenient way to reference data objects by DOM nodes.

Of course, if you "have to remember whery you put it" the DOM references are not the best way to reference your objects. If a simple cache object seems cleaner to you, it probably will be cleaner.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.