Bug Tracker

Opened 10 years ago

Closed 10 years ago

#13151 closed bug (notabug)

Array length pre-allocation in $.when

Reported by: [email protected] Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: git
Keywords: Cc:
Blocked by: Blocking:


The $.when object is the only one, which uses array-preallocation

// ...
		// add listeners to Deferred subordinates; treat others as resolved
		if ( length > 1 ) {
			progressValues = new Array( length );
			progressContexts = new Array( length );
			resolveContexts = new Array( length );
			for ( ; i < length; i++ ) {
				if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
					resolveValues[ i ].promise()
						.done( updateFunc( i, resolveContexts, resolveValues ) )
						.fail( deferred.reject )
						.progress( updateFunc( i, progressContexts, progressValues ) );
				} else {

// ...

since it is not verly likely that when gets passed thousands of parameters this pre-allocation doesn't save much space or will boost performance.

Therefore I propose to save a few bytes here and reduce the 3 pre-allocation lines to something like

			progressValues = [];
			progressContexts = [];
			resolveContexts = [];

I am willing to provide an PR if accepted...

Change History (2)

comment:1 Changed 10 years ago by jaubourg

We don't allocate for speed or space but because we need the array of contexts and arguments to have the right size inside progress handlers. The resolveContexts array could probably be allocated as you suggest but I'm not sure you'd gain a lot of bytes.

comment:2 Changed 10 years ago by dmethvin

Resolution: notabug
Status: newclosed

jaubourg, feel free to make changes from this observation but I'll close the ticket.

Note: See TracTickets for help on using tickets.