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

Optimize rest arguments. #498

Closed
mbebenita opened this issue Aug 21, 2014 · 6 comments
Closed

Optimize rest arguments. #498

mbebenita opened this issue Aug 21, 2014 · 6 comments
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@mbebenita
Copy link

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

@RyanCavanaugh
Copy link
Member

Note that you can work around this by writing:

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

@RyanCavanaugh RyanCavanaugh added this to the Community milestone Aug 22, 2014
@sparecycles
Copy link
Contributor

@RyanCavanaugh How does the extra declaration remove the codegen for the rest array processing and why would that not be a bug in itself?

P.S., the Playground currently generates the same code with or without the extra declaration.

@RyanCavanaugh
Copy link
Member

Typo, I meant to write this:

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

@duncanmak
Copy link

Just a reminder, #518 fixes this issue. When will it get merged into master?

@begincalendar
Copy link
Contributor

As asked by @andy-ms (here), what are the use-cases for having unused rest parameters?

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". and removed Bug A bug in TypeScript labels Jul 8, 2020
@RyanCavanaugh
Copy link
Member

This doesn't really seem needed - the overload trick is sufficient, and most people are not downleveling rest args anymore.

@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants