From c4f144eeffd94c745839b0ced2de9c62cfa9f075 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 15 Sep 2009 07:59:53 +0000 Subject: [PATCH] avoid creating a new data cache if we don't need one. Also, short-circuit the case where $.data is used to get the cache id --- src/data.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/data.js b/src/data.js index 4b557de..c18e73c 100644 --- a/src/data.js +++ b/src/data.js @@ -1,4 +1,5 @@ var expando = "jQuery" + now(), uuid = 0, windowData = {}; +var emptyObject = {}; jQuery.extend({ cache: {}, @@ -10,24 +11,28 @@ jQuery.extend({ windowData : elem; - var id = elem[ expando ], cache = jQuery.cache; + var id = elem[ expando ], cache = jQuery.cache, thisCache; // Compute a unique ID for the element if(!id) id = elem[ expando ] = ++uuid; - // Only generate the data cache if we're - // trying to access or manipulate it - if ( name && !cache[ id ] ) - cache[ id ] = {}; - - var thisCache = cache[ id ]; + // Handle the case where there's no name immediately + if ( !name ) { return id; } + // Avoid generating a new cache unless none exists and we + // want to manipulate it. + if( cache[ id ] ) + thisCache = cache[ id ]; + else if( typeof data === "undefined" ) + thisCache = emptyObject; + else + thisCache = cache[ id ] = {}; + // Prevent overriding the named cache with undefined values if ( data !== undefined ) thisCache[ name ] = data; if(name === true) return thisCache; - else if(name) return thisCache[name]; - else return id; + else return thisCache[name]; }, removeData: function( elem, name ) { -- 1.7.10.4