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

Avoid nesting IIFE for nested namespaces #31072

Open
5 tasks done
mihailik opened this issue Apr 23, 2019 · 1 comment
Open
5 tasks done

Avoid nesting IIFE for nested namespaces #31072

mihailik opened this issue Apr 23, 2019 · 1 comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@mihailik
Copy link
Contributor

mihailik commented Apr 23, 2019

Search Terms

iife namespace nest

Suggestion

Produce a single IIFE for a nested namespace, where applicable.

The point is to reduce onion-like layering in scenarios where it is both trivial and safe to do. There would still be at least 1 level of IIFE wrapping. Every aspect of runtime/semantic/scoping behaviours is preserved as in the current emit.

Example

TypeScript Playground

TypeScript JavaScript
namespace A.B.C {
    export function some() {
        return 'A.B.C.some';
    }
}
var A;
(function (A) {
    var B;
    (function (B) {
        var C;
        (function (C) {
            function some() {
                return 'A.B.C.some';
            }
            C.some = some;
        })(C = B.C || (B.C = {}));
    })(B = A.B || (A.B = {}));
})(A || (A = {}));
desired JavaScript
var A;
(function (A) {
    var B = A.B || (A.B = {});
    var C = B.C || (B.C = {});
    function some() {
        return 'A.B.C.some';
    }
    C.some = some;
})(A || (A = {}));

Use Cases

Nested IIFE can affect performance, epecially in older/constrained environments. Of course, in general case they cannot be avoided, as they create lexical scopes and may have code living in all those nesting levels.

But there are several easy-to-detect scenarios where avoiding IIFE infestation is cheap and easy:

  • namespace A.B.C with single statement of dot-separated nest chain
  • namespace A { namespace B {} } with namespace having a single AST node child of another namespace
    • may have 2 slightly different cases depends on export modifier
  • combination of the above

Note that this feature only affects JS emits, but not parsing neither declaration handling.

Also, this may feel related to #447 Partial modules and output optimization, but the suggestion there is much more broad and heavy to implementation.

Here I do not suggest merging of scopes, just eliminating an easy-to-detect subset of empty scopes that have no code in them.

Non-targeted cases

  1. This feature explicitly DOES NOT suggest merging consecutive scopes, whether in one file or multiple (that may be a valid thing to do elsewhere).
  2. This feature explicitly DOES NOT suggest eliding scopes that have code in them (that would be an error, and should be tested against by a valid implementation).
  3. This feature explicitly DOES NOT intend to elide all possible scopes/layers, only a very few specific easy to check cases.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@mihailik
Copy link
Contributor Author

mihailik commented Apr 23, 2019

May be useful in context:

Blazingly fast parsing, part 2: lazy parsing/Skipping inner functions
(V8 team discussing recent performance optimisations that work around issues caused by multiple nested IIFE)

Each reparse adds at least the cost of parsing the function.
Each reparse adds at least the cost of parsing the function

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Apr 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants