Skip to content

Commit

Permalink
perf(store): replace instanceof Function with typeof (#2247)
Browse files Browse the repository at this point in the history
* perf(store): replace `instanceof Function` with `typeof`

`typeof` avoids prototype chain checks and type coercion, which is faster:

```
instanceof Function x 104,665,819 ops/sec ±4.90% (44 runs sampled)
typeof function x 169,867,932 ops/sec ±6.42% (55 runs sampled)
```

* chore: update CHANGELOG.md
  • Loading branch information
arturovt authored Nov 8, 2024
1 parent f22f4cb commit 82e1215
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ $ npm install @ngxs/store@dev
### To become next patch version

- Fix(store): Prevent writing to state once action handler is unsubscribed [#2231](https://github.com/ngxs/store/pull/2231)
- Performance(store): Replace `instanceof Function` with `typeof` [#2247](https://github.com/ngxs/store/pull/2247)

### 18.1.4 2024-10-23

Expand Down
7 changes: 4 additions & 3 deletions packages/store/src/selectors/selector-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ export function createMemoizedSelectorFn<T extends (...args: any[]) => any>(
creationMetadata: Partial<CreationMetadata> | undefined
) {
const containerClass = creationMetadata && creationMetadata.containerClass;
const wrappedFn = function wrappedSelectorFn(...args: any[]) {
const returnValue = originalFn.apply(containerClass, args);
if (returnValue instanceof Function) {
const wrappedFn = function wrappedSelectorFn() {
// eslint-disable-next-line prefer-rest-params
const returnValue = originalFn.apply(containerClass, <any>arguments);
if (typeof returnValue === 'function') {
const innerMemoizedFn = ɵmemoize.apply(null, [returnValue]);
return innerMemoizedFn;
}
Expand Down

0 comments on commit 82e1215

Please sign in to comment.