Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(switchMapTo): remove tryCatch/errorObject (~2x improvement) #1306

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var RxOld = require('rx');
var RxNew = require('../../../../index');

module.exports = function (suite) {
var resultSelector = function (x, y, ix, iy) { return x + y + ix + iy; };
var oldSwitchMapToWithCurrentThreadScheduler = RxOld.Observable.range(0, 25, RxOld.Scheduler.currentThread)
.flatMapLatest(RxOld.Observable.range(0, 10, RxOld.Scheduler.currentThread), resultSelector);
var newSwitchMapToWithCurrentThreadScheduler = RxNew.Observable.range(0, 25, RxNew.Scheduler.queue)
.switchMapTo(RxNew.Observable.range(0, 10, RxNew.Scheduler.queue), resultSelector);

function _next(x) { }
function _error(e) { }
function _complete() { }
return suite
.add('old switchMapTo with resultSelector and current thread scheduler', function () {
oldSwitchMapToWithCurrentThreadScheduler.subscribe(_next, _error, _complete);
})
.add('new switchMapTo with resultSelector and current thread scheduler', function () {
newSwitchMapToWithCurrentThreadScheduler.subscribe(_next, _error, _complete);
});
};
20 changes: 20 additions & 0 deletions perf/micro/current-thread-scheduler/operators/switchmapto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var RxOld = require('rx');
var RxNew = require('../../../../index');

module.exports = function (suite) {
var oldSwitchMapToWithCurrentThreadScheduler = RxOld.Observable.range(0, 25, RxOld.Scheduler.currentThread)
.flatMapLatest(RxOld.Observable.range(0, 10, RxOld.Scheduler.currentThread));
var newSwitchMapToWithCurrentThreadScheduler = RxNew.Observable.range(0, 25, RxNew.Scheduler.queue)
.switchMapTo(RxNew.Observable.range(0, 10, RxNew.Scheduler.queue));

function _next(x) { }
function _error(e) { }
function _complete() { }
return suite
.add('old switchMapTo with current thread scheduler', function () {
oldSwitchMapToWithCurrentThreadScheduler.subscribe(_next, _error, _complete);
})
.add('new switchMapTo with current thread scheduler', function () {
newSwitchMapToWithCurrentThreadScheduler.subscribe(_next, _error, _complete);
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var RxOld = require('rx');
var RxNew = require('../../../../index');

module.exports = function (suite) {
var resultSelector = function (x, y, ix, iy) { return x + y + ix + iy; };
var oldSwitchMapToWithImmediateScheduler = RxOld.Observable.range(0, 25, RxOld.Scheduler.immediate)
.flatMapLatest(RxOld.Observable.range(0, 10, RxOld.Scheduler.immediate), resultSelector);
var newSwitchMapToWithImmediateScheduler = RxNew.Observable.range(0, 25)
.switchMapTo(RxNew.Observable.range(0, 10), resultSelector);

function _next(x) { }
function _error(e) { }
function _complete() { }
return suite
.add('old switchMapTo with resultSelector and immediate scheduler', function () {
oldSwitchMapToWithImmediateScheduler.subscribe(_next, _error, _complete);
})
.add('new switchMapTo with resultSelector and immediate scheduler', function () {
newSwitchMapToWithImmediateScheduler.subscribe(_next, _error, _complete);
});
};
20 changes: 20 additions & 0 deletions perf/micro/immediate-scheduler/operators/switchmapto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var RxOld = require('rx');
var RxNew = require('../../../../index');

module.exports = function (suite) {
var oldSwitchMapToWithImmediateScheduler = RxOld.Observable.range(0, 25, RxOld.Scheduler.immediate)
.flatMapLatest(RxOld.Observable.range(0, 10, RxOld.Scheduler.immediate));
var newSwitchMapToWithImmediateScheduler = RxNew.Observable.range(0, 25)
.switchMapTo(RxNew.Observable.range(0, 10));

function _next(x) { }
function _error(e) { }
function _complete() { }
return suite
.add('old switchMapTo with immediate scheduler', function () {
oldSwitchMapToWithImmediateScheduler.subscribe(_next, _error, _complete);
})
.add('new switchMapTo with immediate scheduler', function () {
newSwitchMapToWithImmediateScheduler.subscribe(_next, _error, _complete);
});
};
23 changes: 15 additions & 8 deletions src/operator/switchMapTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import {OuterSubscriber} from '../OuterSubscriber';
import {InnerSubscriber} from '../InnerSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
Expand Down Expand Up @@ -69,14 +67,23 @@ class SwitchMapToSubscriber<T, R, R2> extends OuterSubscriber<T, R> {
innerSub: InnerSubscriber<T, R>): void {
const { resultSelector, destination } = this;
if (resultSelector) {
const result = tryCatch(resultSelector)(outerValue, innerValue, outerIndex, innerIndex);
if (result === errorObject) {
destination.error(errorObject.e);
} else {
destination.next(result);
}
this.tryResultSelector(outerValue, innerValue, outerIndex, innerIndex);
} else {
destination.next(innerValue);
}
}

private tryResultSelector(outerValue: T, innerValue: R,
outerIndex: number, innerIndex: number): void {
const { resultSelector, destination } = this;
let result: R2;
try {
result = resultSelector(outerValue, innerValue, outerIndex, innerIndex);
} catch (err) {
destination.error(err);
return;
}

destination.next(result);
}
}