Skip to content

Commit

Permalink
perf(groupBy): remove tryCatch/errorObject for custom tryCatching, 38…
Browse files Browse the repository at this point in the history
…% faster.
  • Loading branch information
benlesh committed Jan 27, 2016
1 parent 869718d commit 40c43f7
Showing 1 changed file with 53 additions and 36 deletions.
89 changes: 53 additions & 36 deletions src/operator/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {Observable} from '../Observable';
import {Subject} from '../Subject';
import {Map} from '../util/Map';
import {FastMap} from '../util/FastMap';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';

/**
* Groups the items emitted by an Observable according to a specified criterion,
Expand Down Expand Up @@ -56,48 +54,67 @@ class GroupBySubscriber<T, K, R> extends Subscriber<T> {
this.add(destination);
}

protected _next(x: T): void {
let key = tryCatch(this.keySelector)(x);
if (key === errorObject) {
this.error(errorObject.e);
} else {
let groups = this.groups;
const elementSelector = this.elementSelector;
const durationSelector = this.durationSelector;

if (!groups) {
groups = this.groups = typeof key === 'string' ? new FastMap() : new Map();
}
protected _next(value: T): void {
let key: any;
try {
key = this.keySelector(value);
} catch (err) {
this.error(err);
return;
}
this._group(value, key);
}

let group = groups.get(key);
private _group(value: T, key: K) {
let groups = this.groups;

if (!group) {
groups.set(key, group = new Subject<T|R>());
let groupedObservable = new GroupedObservable(key, group, this.refCountSubscription);
if (!groups) {
groups = this.groups = typeof key === 'string' ? new FastMap() : new Map();
}

if (durationSelector) {
let duration = tryCatch(durationSelector)(new GroupedObservable<K, R>(key, <any>group));
if (duration === errorObject) {
this.error(errorObject.e);
} else {
this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this)));
}
}
let group = groups.get(key);

this.destination.next(groupedObservable);
}
if (!group) {
groups.set(key, group = new Subject<T|R>());
let groupedObservable = new GroupedObservable(key, group, this.refCountSubscription);

if (elementSelector) {
let value = tryCatch(elementSelector)(x);
if (value === errorObject) {
this.error(errorObject.e);
} else {
group.next(value);
if (this.durationSelector) {
if (!this._tryDuration(key, group)) {
return;
}
} else {
group.next(x);
}

this.destination.next(groupedObservable);
}

if (this.elementSelector) {
this._tryElementSelector(value, group);
} else {
group.next(value);
}
}

private _tryElementSelector(value: T, group: Subject<T | R>) {
let result: any;
try {
result = this.elementSelector(value);
} catch (err) {
this.error(err);
return;
}
group.next(result);
}

private _tryDuration(key: K, group: any): boolean {
let duration: any;
try {
duration = this.durationSelector(new GroupedObservable<K, R>(key, group));
} catch (err) {
this.error(err);
return false;
}
this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this)));
return true;
}

protected _error(err: any): void {
Expand Down

0 comments on commit 40c43f7

Please sign in to comment.