Skip to content

Commit 4ffb1df

Browse files
marjakhmgol
authored andcommitted
Core: Tiny efficiency fix to jQuery.extend / jQuery.fn.extend (#4246)
Read target[name] only when it's needed. In addition to doing the property read-only when needed, this avoids a slow path in V8 (see the issue for more details). Fixes gh-4245 Closes gh-4246
1 parent 13f3cd1 commit 4ffb1df

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

src/core.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ jQuery.extend = jQuery.fn.extend = function() {
156156

157157
// Extend the base object
158158
for ( name in options ) {
159-
src = target[ name ];
160159
copy = options[ name ];
161160

162161
// Prevent never-ending loop
@@ -167,14 +166,17 @@ jQuery.extend = jQuery.fn.extend = function() {
167166
// Recurse if we're merging plain objects or arrays
168167
if ( deep && copy && ( jQuery.isPlainObject( copy ) ||
169168
( copyIsArray = Array.isArray( copy ) ) ) ) {
169+
src = target[ name ];
170170

171-
if ( copyIsArray ) {
172-
copyIsArray = false;
173-
clone = src && Array.isArray( src ) ? src : [];
174-
171+
// Ensure proper type for the source value
172+
if ( copyIsArray && !Array.isArray( src ) ) {
173+
clone = [];
174+
} else if ( !copyIsArray && !jQuery.isPlainObject( src ) ) {
175+
clone = {};
175176
} else {
176-
clone = src && jQuery.isPlainObject( src ) ? src : {};
177+
clone = src;
177178
}
179+
copyIsArray = false;
178180

179181
// Never move original objects, clone them
180182
target[ name ] = jQuery.extend( deep, clone, copy );

0 commit comments

Comments
 (0)