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

Reduce/remove object allocations #12

Open
chadhietala opened this issue Nov 17, 2016 · 0 comments
Open

Reduce/remove object allocations #12

chadhietala opened this issue Nov 17, 2016 · 0 comments
Assignees

Comments

@chadhietala
Copy link

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 in hot paths. To make this a bit more palatable you can write the arguments to a constructor like this.

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

This is equivalent to the following:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants