Description
Bug Report
The Mongoose project recently introduced their own typescript types, therefore deprecating the DefinitelyTyped types.
However, the new types suffer from some big performance issues, greatly slowing down the VS Code developer experience. I've been investigating the problem and noticed that the issue seems to occur from some extends
checks.
Related issues in mongoose repo:
Automattic/mongoose#10349
Automattic/mongoose#10487
Automattic/mongoose#10492
🔎 Search Terms
performance extends instantiations recursiveTypeRelatedTo
🕗 Version & Regression Information
This occurs on TS 4.3.5.
⏯ Playground Link
Unfortunately no playground link, but I made a very simple repro at: https://github.com/andreialecu/typescript-performance-extends
Take a look at the README there.
💻 Code
This diff in mongoose
's types seems to completely eradicate the problem:
diff --git a/types/mongoose.d.ts b/types/mongoose.d.ts
index 9e0ad99..2fab5ff 100644
--- a/types/mongoose.d.ts
+++ b/types/mongoose.d.ts
@@ -1232,7 +1232,7 @@ declare module 'mongoose' {
type PostMiddlewareFunction<ThisType, ResType = any> = (this: ThisType, res: ResType, next: (err?: CallbackError) => void) => void | Promise<void>;
type ErrorHandlingMiddlewareFunction<ThisType, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: (err?: CallbackError) => void) => void;
- class Schema<DocType = Document, M extends Model<DocType, any, any> = Model<any, any, any>, SchemaDefinitionType = undefined, TInstanceMethods = ExtractMethods<M>> extends events.EventEmitter {
+ class Schema<DocType = Document, M = Model<any, any, any>, SchemaDefinitionType = undefined, TInstanceMethods = any> extends events.EventEmitter {
/**
* Create a new schema
*/
The difference between before and after are:
Before:
Identifiers: 45586
Symbols: 76446
Types: 20569
Instantiations: 93884
Memory used: 93011K
Assignability cache size: 5146
Check time: 0.67s
After:
Identifiers: 45582
Symbols: 27736
Types: 710
Instantiations: 818
Memory used: 59482K
Check time: 0.03s
I realize the code after the diff isn't exactly equivalent, but the problem seems to be related to the extends
check. Avoiding extends gets rid of the issue.
Here's a cpu profile:
The 'before' version shows:
🙁 Actual behavior
Slow type checking performance, especially in VS code.
🙂 Expected behavior
Better performance.
I opened this in the hope that there is value in investigating the repro by the TS team, in order to check whether any performance improvements in cases such as this one are possible.