Skip to content

Commit

Permalink
perf(Subscription): use instanceof to avoid megamorphic LoadIC
Browse files Browse the repository at this point in the history
In `Subscription#add()` use `instanceof` to test whether `subscription`
must be wrapped in a `Subscription` (aka for presence of `_addParent`
method), instead of `typeof subscription._addParent === 'function'`,
as the latter is going to turn into a *megamorphic property access*
in any realistic application and cause quite a bit of contention on
the megamorphic stub cache in Node (aka V8).

The `instanceof` is definitely faster, even if the property access
would hit the megamorphic stub cache. For example in the case of a
simple [Angular Universal prerender][1] test, just changing this can
reduce the number of megamorphic stub cache misses by around **2%**.

[1]:
https://gist.githubusercontent.com/bmeurer/5b9480ef1a74c5187180193abc73dcd4/raw/fc5d9a01a6b7fe841efa1c7773ae55e70435d16a/prerender.js
  • Loading branch information
bmeurer committed Jan 28, 2019
1 parent 8253ccd commit 20053d4
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/internal/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class Subscription implements SubscriptionLike {
} else if (this.closed) {
subscription.unsubscribe();
return subscription;
} else if (typeof subscription._addParent !== 'function' /* quack quack */) {
} else if (!(subscription instanceof Subscription)) {
const tmp = subscription;
subscription = new Subscription();
subscription._subscriptions = [tmp];
Expand Down

0 comments on commit 20053d4

Please sign in to comment.