Hi folks,
I recently needed to append some data to dynamically created LI elements
. In my first instance, I used .data()
in a way like
var _newli = $('<li>foobar</li>');
_newli.data('base', 'ball');
// append _newli to an `ul`
that.. was terribly slow. This logic happens in a loop which can easily grow up to 500+ items, it took ages! Sometimes it even broke the javascript execution time frame.
So I changed to $.data()
. Somehow, attaching data to an object with that is like 8x faster than doing it over the .data()
method call. So now it looked like
var _newli = $('<li>foobar</li>');
$.data(_newli[0], 'base', 'ball');
// append _newli to an `ul`
That was/is indeed faster, but still it took like 3-4 seconds(!) to build up all my elements (In my real code there are like 6 calls to $.data per element).
So I was really stuck with that, I asked myself why the heck to use .data()
or $.data()
anyway? I could just attach my data to the DOM object
. So I did
var _newli = $('<li>foobar</li>');
_newli[0].base = 'ball';
// append _newli to an `ul`
Voila, wow to my shock, that was incredibly fast! I couldn't believe that this ran so good without any disadvantage. So that is what is my question all about actually. I didn't find any disadvantage for this technique so far on the net. There are reads about circular references you can create using this way, but AFAIK "only" on IE's and only if you refer to objects
.
Any thoughts experts ?
update
Thanks for the good comments and postings guys. Short update @patrick dw:
You are right, I was passing the underlaying DOM element
when using $.data()
. It does not even work with jQuery objects, at least not as expected.
The idea about using one object and pass it through $.date()
I had myself, but then again I was so shocked about the performance difference I decided just to ignore the .data()
method like forever.