Skip to content

Commit

Permalink
perf(count): remove tryCatch/errorObject for custom tryCatching, 1.84…
Browse files Browse the repository at this point in the history
…M -> 1.97M ops/sec
  • Loading branch information
benlesh committed Jan 27, 2016
1 parent 35caf74 commit 869718d
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/operator/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import {Operator} from '../Operator';
import {Observer} from '../Observer';
import {Subscriber} from '../Subscriber';

import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';

/**
* Returns an observable of a single number that represents the number of items that either:
* Match a provided predicate function, _or_ if a predicate is not provided, the number
Expand Down Expand Up @@ -43,22 +40,30 @@ class CountSubscriber<T> extends Subscriber<T> {
super(destination);
}

protected _next(value: T): void {
const predicate = this.predicate;
let passed: any = true;
if (predicate) {
passed = tryCatch(predicate)(value, this.index++, this.source);
if (passed === errorObject) {
this.destination.error(passed.e);
return;
}
next(value: T): void {
if (this.predicate) {
this._tryPredicate(value);
} else {
this.count++;
}
}

private _tryPredicate(value: T) {
let result: any;

try {
result = this.predicate(value, this.index++, this.source);
} catch (err) {
this.destination.error(err);
return;
}
if (passed) {
this.count += 1;

if (result) {
this.count++;
}
}

protected _complete(): void {
complete(): void {
this.destination.next(this.count);
this.destination.complete();
}
Expand Down

0 comments on commit 869718d

Please sign in to comment.