javascript - jQuerys $.data() vs. DOM Object property -


Recently I need to add some data to the dynamically created LI element . In my first example, I used the .data () such as

  var _newli = $ ('
  • foobar & lt; / li & gt; '); _newli.data ('base', 'ball'); // add _newli to a `ul`
  • . It was slow too low . This logic is in a loop that can easily extend to 500+ items, it took ages! Sometimes it breaks the JavaScript execution time frame too.

    That's why I $ Data changed to . Either way, attaching the data to that object is doing more than 8x with the .data () method call. So it looks like

      var _newli = $ ('
  • foobar & lt; / li & gt;'); $ .data (_newli [0], 'base', 'ball'); // Enclose in one `ul`
  • / is really fast, but still it seemed like 3-4 seconds (!) To make all of my elements ( In my actual code there are $ 6 calls like $ per data).

    So I was really stuck with that, I asked why to use heck .data () or $. Data () anyway? I could attach my data to DOM object . So I did

      var _newli = $ (' 

    Add a voice, wow to my shock, which was incredibly fast! I could not believe that it ran so good without any harm, so that my question is actually what really is on the net so far I have no harm for this technique. Regarding the circular reference , you can create this way, but using the AFAIK "only" IE and only if you refer to objects .

    Any thoughts experts?

    Updates

    Thanks for the good comments and postings, short updates @ Patrick dw:

    You are right, I DOM element $ Data was using . It also does not work with jQuery objects, at least not expected. Use an object and give it to $ I thought about passing through Date () , I was myself, but then I was so amazed about the difference in performance that I just like the method is always the same.

    You are correct about circular reference , this is outside of IE There is no problem, and in IE it becomes only an issue when Javascript refers to a DOM object, and a JS object is assigned to one of the properties of a DOM object. I believe this can only be resolved by canceling any reference to JS in the DOM object.

    $ (). There is a very complex complex wrapper for data () method $ Data () (see jQuery.fn.data :, which in turn calls jQuery.data :), then that secondary person Especially if it is to be done 500 times.

    In this situation, $ (). Do not exceed data ('foo', 'bar') method el.foo = 'bar' What I do the fastest.


    Comments

    Popular posts from this blog

    Eclipse CDT variable colors in editor -

    AJAX doesn't send POST query -

    wpf - Custom Message Box Advice -