Skip to content

Commit

Permalink
perf(ReplaySubject): little performance adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsik committed Jun 19, 2017
1 parent e98c337 commit a4e146e
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/ReplaySubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ export class ReplaySubject<T> extends Subject<T> {
}

private nextInfiniteTimeWindow(value: T): void {
this._events.push(value);
this._trimBufferSize();
const _events = this._events;
_events.push(value);
// Since this method is invoked in every next() call than the buffer
// can overgrow the max size only by one item
if (_events.length > this._bufferSize) {
_events.shift();
}

super.next(value);
}
Expand All @@ -49,13 +54,12 @@ export class ReplaySubject<T> extends Subject<T> {
const _infiniteTimeWindow = this._infiniteTimeWindow;
const _events = _infiniteTimeWindow ? this._events : this._trimBufferThenGetEvents();
const scheduler = this.scheduler;
const len = _events.length;
let subscription: Subscription;

if (this.closed) {
throw new ObjectUnsubscribedError();
} else if (this.hasError) {
subscription = Subscription.EMPTY;
} else if (this.isStopped) {
} else if (this.isStopped || this.hasError) {
subscription = Subscription.EMPTY;
} else {
this.observers.push(subscriber);
Expand All @@ -66,7 +70,6 @@ export class ReplaySubject<T> extends Subject<T> {
subscriber.add(subscriber = new ObserveOnSubscriber<T>(subscriber, scheduler));
}

const len = _events.length;
if (_infiniteTimeWindow) {
for (let i = 0; i < len && !subscriber.closed; i++) {
subscriber.next(<T>_events[i]);
Expand Down Expand Up @@ -96,7 +99,7 @@ export class ReplaySubject<T> extends Subject<T> {
const _windowTime = this._windowTime;
const _events = <ReplayEvent<T>[]>this._events;

let eventsCount = _events.length;
const eventsCount = _events.length;
let spliceCount = 0;

// Trim events that fall out of the time window.
Expand All @@ -120,14 +123,6 @@ export class ReplaySubject<T> extends Subject<T> {
return _events;
}

private _trimBufferSize(): void {
const { _events } = this;

for (let i = 0; i < _events.length - this._bufferSize; i++) {
_events.shift();
}
}

}

class ReplayEvent<T> {
Expand Down

0 comments on commit a4e146e

Please sign in to comment.