Skip to content

Inefficient compilation for fields initialization without explicit constructor (target=ES6) #10175

Closed
@chancancode

Description

@chancancode

TypeScript Version: 1.8 and nightly (2.1.0-dev.20160805)

Code

class A {}

class B extends A {
  private foo = 1;
}

Expected behavior:

// compiles into...
class A {}

class B extends A {
  constructor() {
    super(...arguments);
    this.foo = 1;
  }
}

Actual behavior:

// compiles into...
class A {}

class B extends A {
  constructor(...args) {
    super(...args);
    this.foo = 1;
  }
}

If you take the first compilation and feed it into Babel (loose mode), you will get (more or less)...

var A = function A() {
};

var B = function (_A) {
  _inherits(B, _A);

  function B() {
    _A.apply(this, arguments);

    this.foo = 1;
  }

  return B;
}(A);

But if you take the second version, you will get...

var A = function A() {
};

var B = function (_A) {
  _inherits(B, _A);

  function B() {
    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _A.call.apply(_A, [this].concat(args));

    this.foo = 1;
  }

  return B;
}(A);

Which is much less efficient – it allocates two extra arrays, reify arguments into an array, extra concat and ....call.apply (o_0).

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions