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

A 10 line script takes 40 seconds to transpile #18005

Closed
nchanged opened this issue Aug 24, 2017 · 3 comments
Closed

A 10 line script takes 40 seconds to transpile #18005

nchanged opened this issue Aug 24, 2017 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@nchanged
Copy link

nchanged commented Aug 24, 2017

Hi!
We use typescript to transpile some es6 modules. I recently found a very annoying piece of code in moment library that just hangs typescript for 40 (!!!!) seconds

TypeScript Version: 2.6.0-dev.20170824

Code

const ts = require("typescript")
const start = new Date().getTime()
let result = ts.transpileModule(`
let seconds, minutes, hours = 0;
let thresholds = {}
var a = seconds <= thresholds.ss && ['s', seconds] ||
    seconds < thresholds.s && ['ss', seconds] ||
    minutes <= 1 && ['m'] ||
    minutes < thresholds.m && ['mm', minutes] ||
    hours <= 1 && ['h'] ||
    hours < thresholds.h && ['hh', hours] ||
    days <= 1 && ['d'] ||
    days < thresholds.d && ['dd', days] ||
    months <= 1 && ['M'] ||
    months < thresholds.M && ['MM', months] ||
    years <= 1 && ['y'] || ['yy', years];

`, {
    compilerOptions: {
        module: "es6"
    }
});
const end = new Date().getTime();
console.log(result);
console.log("Took", (end - start));

I played around with it for a bit, and discovered that removing years <= 1 && ['y'] || ['yy', years]; (the last line) makes it compile pretty much instantly.

It's easy to reproduce: copy the snippet, create a js file (or ts) doesn't matter, and execute it.

Thanks a lot!

@mhegazy mhegazy added the Bug A bug in TypeScript label Aug 24, 2017
@mhegazy mhegazy added this to the TypeScript 2.6 milestone Aug 24, 2017
@grantila
Copy link

var a =
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4];

took 4.5 minutes to compile.

Seems both

var a =
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    .
    .
    .;

and

var a =
    x && [1, 2, ...] ||
    x && [1, 2, ...] ||
    x && [1, 2, ...] ||
    x && [1, 2, ...];

individually causes exponential compile time. Combining them obviously causes exponential^2 compile times.

I get the same result on 2.4.

@ahejlsberg ahejlsberg assigned ahejlsberg and unassigned sandersn Aug 30, 2017
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Aug 31, 2017
@ahejlsberg
Copy link
Member

Fixed in #18174.

Until the fix ships, a simple workaround is to add a type annotation to the variable being initialized, e.g.

var a: number[] =
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4] ||
    x && [1, 2, 3, 4];

@nchanged
Copy link
Author

👍

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants