Interesting jquery.data.

Source: Internet
Author: User
Tags hasownproperty

Just looking at the method of Jquery.data method, its official website http://api.jquery.com/jQuery.data/on it has a variety of different evaluation, have considered $. The speed of data and the speed of the el.data who are faster, have to consider. Data can store as much as possible, and consider whether the garbage collector can reclaim these. data and so on.

About the $.data () and El.data () speed of the statement on the Http://jsperf.com/jquery-data-vs-jqueryselection-data this page has a simulation test, after several tests $.data () is almost 3 times times the speed of El.data (). And the poor speed of discovering $.data () and El.data () in the context and browsers below is not a little bit. Before using the data method is to use El.data (), because it is more clear meaning, it is easier to understand what we are doing. It seems that the future still have to go the standard road, in the official website only saw the introduction of $.data (), so later still use $.data () bar.

On the issue of memory recycling for. Data, there is a lot of memory leaks on the web, in the Http://forum.jquery.com/topic/data-object-and-memory-leak of jquery forum:

The current $.data implementation is optimized to host objects only, and not for native objects.
In fact, it uses the cache object to store all related the data and a expando with numerical values to keep references.
The need of such solution is unquestionable and it fits down because elements are "normally" by using. Remove () That also cleans respective data. Native objects, in other hand, are not ' eliminated ' with such called, and related data exists until the ' window is closed.

Of course, this problem also exists with the event API, which uses data API to store handlers ...

The $.data () method is applied to the host object, the run is optimized, but the method is used on the local object, and the result may not be satisfactory. An element can normally be removed by using the. Remove () method and the respective data is purged. For local objects, however, this is not completely deleted, and the associated data continues until the window object closes. The same problem also occurs on the event object, because the event handler (handlers) is also used to store the method.

Robert.katic has overridden the. Data () method and added the. Removedata () Method:

(function ($) {var expando = "JQuery" + (new Date). GetTime (), hasOwnProperty = Object.prototype.hasOwnPropert 
    Y, _data = $.data, _removedata = $.removedata; 
        $.data = function (obj, name, data) {if (Obj.nodetype) {return _data (obj, name, data); 
        var thiscache, Hascache = Hasownproperty.call (obj, expando); 
        if (!hascache && typeof name = = = "string" && data = = undefined) {return undefined; 
  
       } if (typeof name = = = "Object") {obj[expando] = $.extend (true, {}, name); 
        else if (!hascache) {obj[expando] = {}; 
        } Thiscache = obj[expando]; 
            if (typeof name = = = "string") {if (data!== undefined) {thiscache[name] = data; 
        return thiscache[name]; 
    return thiscache; 
  
    }; $.removedata = FuNction (obj, name) {if (Obj.nodetype) {return _removedata (obj, name);  } if (name) {if (Hasownproperty.call (obj, expando)) {delete obj[expando] [ 
                Name]; 
                if ($.isemptyobject (Obj[expando])) {delete obj[expando]; 
        }} else {delete obj[expando]; 
} 
    }; }) (JQuery);
This way you don't have to worry about the memory leaks of data ().

But an interesting question arose, said on http://news.ycombinator.com/item?id=3367510 250 days ago: Don t use the jquery. Data () Method,use. attr () instead I am strange, continue to look down, originally this is a bug, I tried one of the examples, also did not find any strange things appear. Experiments on the http://jsfiddle.net/4bd23/, there is a problem, in their own browser test also have problems. Also found that. Data () can also be set using the <div id= "foo" data-myinline= "1.410" ></div> in such a way that a value can be obtained using Foo.data (' Myinline ').

There are such interesting problems appearing that later use. Data () or you need to be more careful. All right, get ready for bed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.