Skip to content

Commit c4f144e

Browse files
committed
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
1 parent 19cd84c commit c4f144e

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/data.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var expando = "jQuery" + now(), uuid = 0, windowData = {};
2+
var emptyObject = {};
23

34
jQuery.extend({
45
cache: {},
@@ -10,24 +11,28 @@ jQuery.extend({
1011
windowData :
1112
elem;
1213

13-
var id = elem[ expando ], cache = jQuery.cache;
14+
var id = elem[ expando ], cache = jQuery.cache, thisCache;
1415

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

18-
// Only generate the data cache if we're
19-
// trying to access or manipulate it
20-
if ( name && !cache[ id ] )
21-
cache[ id ] = {};
22-
23-
var thisCache = cache[ id ];
19+
// Handle the case where there's no name immediately
20+
if ( !name ) { return id; }
2421

22+
// Avoid generating a new cache unless none exists and we
23+
// want to manipulate it.
24+
if( cache[ id ] )
25+
thisCache = cache[ id ];
26+
else if( typeof data === "undefined" )
27+
thisCache = emptyObject;
28+
else
29+
thisCache = cache[ id ] = {};
30+
2531
// Prevent overriding the named cache with undefined values
2632
if ( data !== undefined ) thisCache[ name ] = data;
2733

2834
if(name === true) return thisCache;
29-
else if(name) return thisCache[name];
30-
else return id;
35+
else return thisCache[name];
3136
},
3237

3338
removeData: function( elem, name ) {

0 commit comments

Comments
 (0)