Skip to content

Commit

Permalink
perf(Stream): improve performance of Observer methods in Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Medeiros committed Feb 26, 2016
1 parent 7ff9fd0 commit 465f22d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
8 changes: 4 additions & 4 deletions perf/filter-map-reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var options = {
};

suite
.add('xstream', function(deferred) {
runners.runXStream(deferred,
xstream.from(a).filter(even).map(add1).fold(sum, 0).last());
}, options)
.add('most', function(deferred) {
runners.runMost(deferred, most.from(a).filter(even).map(add1).reduce(sum, 0));
}, options)
Expand All @@ -38,10 +42,6 @@ suite
runners.runRx5(deferred,
rxjs.Observable.fromArray(a).filter(even).map(add1).reduce(sum, 0));
}, options)
.add('xstream', function(deferred) {
runners.runXStream(deferred,
xstream.from(a).filter(even).map(add1).fold(sum, 0).last());
}, options)
.add('kefir', function(deferred) {
runners.runKefir(deferred, kefirFromArray(a).filter(even).map(add1).scan(sum, 0).last());
}, options)
Expand Down
24 changes: 18 additions & 6 deletions src/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,34 @@ export class Stream<T> implements Observer<T> {

next(x: T): void {
const len = this.observers.length;
for (let i = len - 1; i >= 0; i--) {
this.observers[i].next(x);
if (len === 1) {
this.observers[0].next(x);
} else {
for (let i = 0; i < len; i++) {
this.observers[i].next(x);
}
}
}

error(err: any): void {
const len = this.observers.length;
for (let i = len - 1; i >= 0; i--) {
this.observers[i].error(err);
if (len === 1) {
this.observers[0].error(err);
} else {
for (let i = 0; i < len; i++) {
this.observers[i].error(err);
}
}
}

complete(): void {
const len = this.observers.length;
for (let i = len - 1; i >= 0; i--) {
this.observers[i].complete();
if (len === 1) {
this.observers[0].complete();
} else {
for (let i = 0; i < len; i++) {
this.observers[i].complete();
}
}
}

Expand Down

0 comments on commit 465f22d

Please sign in to comment.