Skip to content

Reduce/remove object allocations #12

Description

@chadhietala

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.

Activity

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions