Skip to content
Permalink
Browse files

Refactor jQuery.data a bit to reduce property lookups

  - Also added jQuery.isEmptyObject
  • Loading branch information
wycats committed Jul 16, 2009
1 parent d36d224 commit 190812c3be99bde536d373b6b6ab65cfe053a532
Showing with 21 additions and 23 deletions.
  1. +6 −0 src/core.js
  2. +15 −23 src/data.js
@@ -291,6 +291,12 @@ jQuery.extend({
return this.constructor.call(obj) === Object;
},

isEmptyObject: function( obj ) {
var name = "";
for(name in obj) break;
return !name;
},

// check if an element is in a (or is an) XML document
isXMLDoc: function( elem ) {
return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
@@ -8,49 +8,41 @@ jQuery.extend({
windowData :
elem;

var id = elem[ expando ];
var id = elem[ expando ], cache = jQuery.cache;

// Compute a unique ID for the element
if ( !id )
id = elem[ expando ] = ++uuid;
if(!id) id = elem[ expando ] = ++uuid;

// Only generate the data cache if we're
// trying to access or manipulate it
if ( name && !jQuery.cache[ id ] )
jQuery.cache[ id ] = {};
if ( name && !cache[ id ] )
cache[ id ] = {};

// Prevent overriding the named cache with undefined values
if ( data !== undefined )
jQuery.cache[ id ][ name ] = data;
var thisCache = cache[ id ];

if(name === true) return jQuery.cache[ id ]
// Prevent overriding the named cache with undefined values
if ( data !== undefined ) thisCache[ name ] = data;

// Return the named cache data, or the ID for the element
return name ?
jQuery.cache[ id ][ name ] :
id;
if(name === true) return thisCache
else if(name) return thisCache[name]
else return id
},

removeData: function( elem, name ) {
elem = elem == window ?
windowData :
elem;

var id = elem[ expando ];
var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ];

// If we want to remove a specific section of the element's data
if ( name ) {
if ( jQuery.cache[ id ] ) {
if ( thisCache ) {
// Remove the section of cache data
delete jQuery.cache[ id ][ name ];
delete thisCache[ name ];

// If we've removed all the data, remove the element's cache
name = "";

for ( name in jQuery.cache[ id ] )
break;

if ( !name )
if( jQuery.isEmptyObject(thisCache) )
jQuery.removeData( elem );
}

@@ -67,7 +59,7 @@ jQuery.extend({
}

// Completely remove the data cache
delete jQuery.cache[ id ];
delete cache[ id ];
}
},
queue: function( elem, type, data ) {

0 comments on commit 190812c

Please sign in to comment.
You can’t perform that action at this time.