Skip to main content

Bug Tracker

Side navigation

#13151 closed bug (notabug)

Opened January 04, 2013 02:24PM UTC

Closed January 26, 2013 09:08PM UTC

Array length pre-allocation in $.when

Reported by: 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...

Attachments (0)
Change History (2)

Changed January 04, 2013 02:37PM UTC by jaubourg comment:1

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.

Changed January 26, 2013 09:08PM UTC by dmethvin comment:2

resolution: → notabug
status: newclosed

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