最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prevent Typescript compiler from checking entire classes to save time? - Stack Overflow

programmeradmin6浏览0评论

Typescript pilation is taking a long time to run, so I used generateTrace from

It showed that most of the time was paring plicated classes with their subclasses. E.g. one of the classes is the base Objection model (.js), which is the base class that all my models inherit.

E.g. I have code like:

class User extends Model {}

function doSomething(model: typeof Model) {}

doSomething(User);

I have ~50 models. I think the first time TS encounters each model is slow, then it caches it. It takes about 5s for TS to pare a specific model with the base Model. Model uses types from several libraries. Here's a screenshot of the trace, it takes 5min just to pare each model with Model:

Is there a way to get TS to skip paring Model with itself? I.e. since User extends Model, there's no need to check the fields it inherited.

Edit:

I reduced the time to check the 50 models from 5min to 30s. I have a map of models:

type ModelType = 'user' | ...

type ModelsMap = {
  user: User,
  ...
};

getModel<T extends ModelType>(type: T): ModelsMap[T] {}

This is slow because ModelsMap[T] is a union of all the models. It became faster if I return the base model when T is an union of all the model types:

type TypeToModel<T extends ModelType> = ModelType extends T ? Model : ModelsMap[T];

getModel<T extends ModelType>(type: T): TypeToModel<T> {}

However, it would still be nice to know if there are hacks to make paring subclass faster. E.g. if it's possible to disable structural typing and use nominal for subclasses.

Typescript pilation is taking a long time to run, so I used generateTrace from https://github./microsoft/TypeScript/pull/40063

It showed that most of the time was paring plicated classes with their subclasses. E.g. one of the classes is the base Objection model (https://github./vincit/objection.js), which is the base class that all my models inherit.

E.g. I have code like:

class User extends Model {}

function doSomething(model: typeof Model) {}

doSomething(User);

I have ~50 models. I think the first time TS encounters each model is slow, then it caches it. It takes about 5s for TS to pare a specific model with the base Model. Model uses types from several libraries. Here's a screenshot of the trace, it takes 5min just to pare each model with Model:

Is there a way to get TS to skip paring Model with itself? I.e. since User extends Model, there's no need to check the fields it inherited.

Edit:

I reduced the time to check the 50 models from 5min to 30s. I have a map of models:

type ModelType = 'user' | ...

type ModelsMap = {
  user: User,
  ...
};

getModel<T extends ModelType>(type: T): ModelsMap[T] {}

This is slow because ModelsMap[T] is a union of all the models. It became faster if I return the base model when T is an union of all the model types:

type TypeToModel<T extends ModelType> = ModelType extends T ? Model : ModelsMap[T];

getModel<T extends ModelType>(type: T): TypeToModel<T> {}

However, it would still be nice to know if there are hacks to make paring subclass faster. E.g. if it's possible to disable structural typing and use nominal for subclasses.

Share Improve this question edited Jan 5, 2022 at 14:41 captain-yossarian from Ukraine 33.2k5 gold badges38 silver badges75 bronze badges asked Dec 23, 2021 at 8:51 Leo JiangLeo Jiang 26.3k59 gold badges177 silver badges328 bronze badges 5
  • Could you please provide more information how you measure it? I did not know that it is possible to debug ts in chrome. Thank you – captain-yossarian from Ukraine Commented Dec 23, 2021 at 9:09
  • The author of generateTrace has instructions here: github./microsoft/TypeScript/pull/… The trace shows that each parison between the base model class and a subclass takes a few seconds. – Leo Jiang Commented Dec 24, 2021 at 0:41
  • I imagine you've already taken a look at the piler API, but if not, then this is a great time. You could use incremental pilation and cache AST outputs (e.g. according to a digest of each module's contents). I don't know if a project like that already exists, but I can't be the first one to imagine wanting to cache pilation results. – jsejcksn Commented Dec 24, 2021 at 2:00
  • 3 For others ing across this question, Typescript performance has generally been causing issues for Objection since at least TS 4.4, worsening with 4.5. 1 2 3. Since this issue has to do with the internal typings of Objection (other ORMs and libraries aren't having this problem) I remend opening an issue. – JH- Commented Dec 30, 2021 at 6:59
  • I would like to know how you traced this process. – khush Commented Jan 6, 2022 at 5:41
Add a ment  | 

2 Answers 2

Reset to default 3 +50

I'm not sure it will be very helpful in your case, but you can dramatically speed up the process by allowing the incremental flag to your tsc mand:

// package.json
"tsc": "tsc -v && tsc --noEmit --skipLibCheck --incremental",

This will create a tsconfig.tsbuildinfo file, that is actually a graph of your codebase. Typescript will then only pare the entries you have changed. The first pilation will be slow, because it will generate the graph, but afterwards, the pilation time will be cut by 3 at least.

Some tsc flags that can help with typescript pilation speed are skipLibCheck and incremental

If that is still not fast enough, consider using a different piler than tsc (at least in-dev). Some examples of what you could use:

  • ts-node-dev
  • ts-node
  • swc
发布评论

评论列表(0)

  1. 暂无评论