Bug Tracker

Modify

Ticket #9994 (closed enhancement: wontfix)

Opened 3 years ago

Last modified 2 years ago

Optimization improvement in jQuery.extend

Reported by: anonymous Owned by: rwaldron
Priority: low Milestone:
Component: core Version: 1.6.2
Keywords: Cc:
Blocking: Blocked by:

Description

On line 313 of core.js:

 https://github.com/jquery/jquery/blob/master/src/core.js#L313

there is a check for:

typeof target === "boolean"

However, target has been set by:

target = arguments[0] || {},

so in case it's a boolean, it's always true (if arguments[0] was false, target is {}). Checking for true is much faster than a typeof check and then comparing strings, especially on IE:

 http://jsperf.com/true-vs-typeof

Thanks for considering this optimization.

Change History

comment:1 Changed 3 years ago by rwaldron

  • Owner set to rwaldron
  • Priority changed from undecided to low
  • Status changed from new to assigned
  • Component changed from unfiled to core
  • Milestone changed from None to 1.6.3
Last edited 3 years ago by rwaldron (previous) (diff)

comment:2 Changed 3 years ago by timmywil

Seems to me that this will not set the target correctly if both false and a target is passed to extend. That may not happen very often, but would be a regression.

$.extend( false, { some: "target" }, { more: "stuff" } );

comment:3 Changed 3 years ago by dmethvin

More often than I would have expected, that's fer sure.

 http://google.com/codesearch#search/&q=\$\.extend\(\s*false

There are probably more of the form $.extend(iCanHazDeep, defaults, options) but they're too hard to grep.

comment:4 Changed 3 years ago by rwaldron

If arguments[0] is any kind of falsy, it's not boolean by the time it reaches that condition.

comment:5 Changed 3 years ago by rwaldron

Sorry, by "it" I meant the target var

comment:6 Changed 3 years ago by rwaldron

  • Status changed from assigned to closed
  • Resolution set to wontfix

Turns out this is a performance hit, so no thanks.

comment:7 Changed 3 years ago by timmywil

This is now neither here nor there, but let me explain further. I don't think this would work anyway because target needs to get assigned to arguments[1] even if arguments[0] is false. It can't just be an empty plain object. The only place that happens is from within the typeof conditional, so it can't change unless that assignment is added somewhere else and at that point we're talking about changing more than just the conditional.

comment:8 Changed 3 years ago by dmethvin

  • Milestone 1.6.3 deleted

comment:9 Changed 2 years ago by rwaldron

#10867 is a duplicate of this ticket.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.