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: | maggus.staab@googlemail.com | Owned by: | |
---|---|---|---|
Priority: | undecided | Milestone: | None |
Component: | unfiled | Version: | git |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
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 { --remaining; } } } // ...
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...
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.