Skip to content

Optimize rest arguments. #498

Closed as not planned
Closed as not planned
@mbebenita

Description

@mbebenita

The TypeScript compiler generates the following code for rest arguments:

function foo(x, y, z, ...rest) {
  return;
}

function foo(x, y, z) {
  var rest = [];
  for (var i = 0; i < arguments.length - 3; i++) {
    rest[i] = arguments[i + 3];
  }
  return;
}

Although rest is unused in the body of the function, JS engines can't easily dead code eliminate the allocation of the rest array and the subsequent for loop due to the possibility of Array.prototype being mutated:

Object.defineProperty(Array.prototype, 42, { set: function() { alert(this[41]); }});

It's much easier for the TypeScript compiler to avoid emitting this code when it is not necessary.

See more details:

https://bugzilla.mozilla.org/show_bug.cgi?id=1056446

Metadata

Metadata

Assignees

No one assigned

    Labels

    Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Help WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions