Skip to content

Commit

Permalink
Core: Tiny efficiency fix to jQuery.extend / jQuery.fn.extend (#4246)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
marjakh authored and mgol committed Dec 12, 2018
1 parent 13f3cd1 commit 4ffb1df
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/core.js
Expand Up @@ -156,7 +156,6 @@ jQuery.extend = jQuery.fn.extend = function() {


// Extend the base object // Extend the base object
for ( name in options ) { for ( name in options ) {
src = target[ name ];
copy = options[ name ]; copy = options[ name ];


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


if ( copyIsArray ) { // Ensure proper type for the source value
copyIsArray = false; if ( copyIsArray && !Array.isArray( src ) ) {
clone = src && Array.isArray( src ) ? src : []; clone = [];

} else if ( !copyIsArray && !jQuery.isPlainObject( src ) ) {
clone = {};
} else { } else {
clone = src && jQuery.isPlainObject( src ) ? src : {}; clone = src;
} }
copyIsArray = false;


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

0 comments on commit 4ffb1df

Please sign in to comment.