…to contain the cache ID to avoid it being JSON serialized.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -46,13 +46,30 @@ jQuery.extend({ | ||
// Avoid generating a new cache unless none exists and we | ||
// want to manipulate it. | ||
if ( typeof name === "object" ) { | ||
cache[ id ] = jQuery.extend(true, {}, name); | ||
if ( isNode ) { | ||
cache[ id ] = jQuery.extend(true, {}, name); | ||
} else { | ||
cache[ id ] = function() { | ||
return jQuery.extend(true, {}, name); | ||
} | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jeresig
Member
|
||
|
||
} else if ( !cache[ id ] ) { | ||
cache[ id ] = {}; | ||
if ( isNode ) { | ||
cache[ id ] = {}; | ||
} else { | ||
var store = {}; | ||
cache[ id ] = function() { | ||
return store; | ||
} | ||
} | ||
|
||
} | ||
|
||
thisCache = cache[ id ]; | ||
if ( !isNode ) { | ||
thisCache = thisCache(); | ||
} | ||
|
||
// Prevent overriding the named cache with undefined values | ||
if ( data !== undefined ) { | ||
@@ -71,8 +88,12 @@ jQuery.extend({ | ||
windowData : | ||
elem; | ||
|
||
var id = elem[ jQuery.expando ], cache = jQuery.cache, | ||
isNode = elem.nodeType, thisCache = isNode ? cache[ id ] : id; | ||
var isNode = elem.nodeType, | ||
id = elem[ jQuery.expando ], cache = jQuery.cache; | ||
if ( id && !isNode ) { | ||
id = id(); | ||
} | ||
var thisCache = cache[ id ]; | ||
|
||
// If we want to remove a specific section of the element's data | ||
if ( name ) { | ||
5 comments
on commit 9faab0b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw. Is there a reason to not simply using a function as the object cache and setting properties to that function?
Besides avoiding one function call, that would make the .data() function something simpler eliminating some isNode checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea.. but that would make fields of the function look like data, like .length
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair reason. How about to store cache object as the "store" property of the function? At least that would avoid one function call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be precise it would replace a function call with a field reference. Net positive I'm sure, but not sure how much. I'd want to get an idea of the perf difference. Either way it's probably a negligible difference, only making a difference if a page is doing tons and tons of data() calls very quickly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, in cases where JS is not operating on the DOM, function calls becomes much more significant, and since we are discussing about not-node cases, avoiding not beneficial function calls seams preferable to me.
In case of a node, the copy is made immediately (good), but for other objects, the copy is made on (each!) reading operations!
Am I missing something?