aboutsummaryrefslogtreecommitdiffstats
path: root/src/core.js
diff options
context:
space:
mode:
authorMarja Hölttä <marja.holtta@gmail.com>2018-12-12 17:13:18 +0100
committerMichał Gołębiowski-Owczarek <m.goleb@gmail.com>2018-12-12 17:13:18 +0100
commit4ffb1df8e4738eb86bde429ec20efc7394e5e497 (patch)
treed3ce820a138d3ff50437202897b9b8c4d3bbeebe /src/core.js
parent13f3cd1611d7905c6fadcf2f8a533096b347a6ad (diff)
downloadjquery-4ffb1df8e4738eb86bde429ec20efc7394e5e497.tar.gz
jquery-4ffb1df8e4738eb86bde429ec20efc7394e5e497.zip
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
Diffstat (limited to 'src/core.js')
-rw-r--r--src/core.js14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/core.js b/src/core.js
index 633d09381..84f9afe13 100644
--- a/src/core.js
+++ b/src/core.js
@@ -156,7 +156,6 @@ jQuery.extend = jQuery.fn.extend = function() {
// Extend the base object
for ( name in options ) {
- src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
@@ -167,14 +166,17 @@ jQuery.extend = jQuery.fn.extend = function() {
// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( jQuery.isPlainObject( copy ) ||
( copyIsArray = Array.isArray( copy ) ) ) ) {
+ src = target[ name ];
- if ( copyIsArray ) {
- copyIsArray = false;
- clone = src && Array.isArray( src ) ? src : [];
-
+ // Ensure proper type for the source value
+ if ( copyIsArray && !Array.isArray( src ) ) {
+ clone = [];
+ } else if ( !copyIsArray && !jQuery.isPlainObject( src ) ) {
+ clone = {};
} else {
- clone = src && jQuery.isPlainObject( src ) ? src : {};
+ clone = src;
}
+ copyIsArray = false;
// Never move original objects, clone them
target[ name ] = jQuery.extend( deep, clone, copy );