linkedin / linkedin/spaniel

Reduce/remove object allocations

Open
#12 0 comments 0 reactions 1 assignee Claimed by @asakusuma View on GitHub
performance
Dominant language
JavaScript
Stars
92
Forks
34
PR merge metrics
No merged PRs in 30d

Description

I feel like there are a lot places where we can either recycle objects or just not allocate at all. One quick actionable item is to remove any POJO arguments e.g.

```
new Foo({ bar: 'bar', baz: 1 });
```

These are typically short lived objects that need to be GCd since all we typically do is peel them off in the constructor. Possibly run into [this issue](https://docs.google.com/document/d/1VEeXn6BfKmVkYpfMuxNs3otLLNPIxIVA1cLoSwGMIxI/edit) in hot paths. To make this a bit more palatable you can write the arguments to a constructor like this.

```ts
class Foo {
public someNum: number;
constructor(
public bar: string = 'default',
private baz: number = 0
) {
this.sumNumber = baz * 10;
}
}
```

This is equivalent to the following:

```ts
class Foo {
public bar: string = 'default';
private baz: number = 0;
public sumNumber: number;
constructor(bar: string, baz: number) {
this.bar = bar;
this.baz = baz;
this.sumNumber = baz * 10;
}
}
```

Since we have types getting argument position correct is less of an issue. Typically if there are more than 4 args to a class there is likely another object in there hiding.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.