Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
perf($q): small $q performance optimization
Browse files Browse the repository at this point in the history
Only generate a new promise when `then` receives some non-undefined parameter

Closes #12535
  • Loading branch information
lgalfaso committed Aug 11, 2015
1 parent f827a8e commit 6838c97
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/promises-aplus/promises-aplus-test-adapter.js
Expand Up @@ -4,6 +4,8 @@
var isFunction = function isFunction(value){return typeof value == 'function';};
var isPromiseLike = function isPromiseLike(obj) {return obj && isFunction(obj.then);};
var isObject = function isObject(value){return value != null && typeof value === 'object';};
var isUndefined = function isUndefined(value) {return typeof value === 'undefined';};

var minErr = function minErr (module, constructor) {
return function (){
var ErrorConstructor = constructor || Error;
Expand Down
3 changes: 3 additions & 0 deletions src/ng/q.js
Expand Up @@ -274,6 +274,9 @@ function qFactory(nextTick, exceptionHandler) {

extend(Promise.prototype, {
then: function(onFulfilled, onRejected, progressBack) {
if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) {
return this;
}
var result = new Deferred();

this.$$state.pending = this.$$state.pending || [];
Expand Down

3 comments on commit 6838c97

@matsko
Copy link
Contributor

@matsko matsko commented on 6838c97 Aug 14, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No tests?

@gkalpak
Copy link
Member

@gkalpak gkalpak commented on 6838c97 Aug 30, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lgalfaso, won't this have the (breaking) side-effect of not invoking scheduleProcessQueue(this.$$state) under certain circumstances ?

@lgalfaso
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gkalpak there is no side-effect. This is the reasoning:

  • if this.$$state.pending is null or [] then nothing is added, so no side-effect.
  • if this.$$state.pending has elements and this.$$state.status <= 0 then scheduleProcessQueue would not get called, so no side-effects.
  • if this.$$state.pending has elements and this.$$state.status > 0 and the elements where added after the change of status, then there is already a call to scheduleProcessQueue pending on the previous then call, so no side-effects.
  • if this.$$state.pending has elements and this.$$state.status > 0 and the elements where not added after the change of status, then the change of status would call scheduleProcessQueue to schedule resolving the pending promises, so no side-effects.

The only side-effect is when running tests and hopefully that is properly reflected with #12705

Please sign in to comment.