TL;DR
What are hidden or not well-known mechanisms that can impact the check time of the TS Server ?
Context
Library
I’ve written a lib that does quite some cool magic. I’ve followed TS guidelines for performance but I get lower performance than I would like regardless.
I used tracing to see where the issue was, and it boiled down to every evaluation of an expression that uses my lib. Yet, when I use analyze-trace
, it says nothing is wrong.
My lib require you to define some big boy type that looks like the following :
type BigBoy = {
store: {
library: {
book: [ DocDef<`book::${string}`, { title: string; authors: string[] }> ]
}
}
}
Note that while the type is large, it is not complex and tracing did not report any significant time spent here. Also, that type rarely change.
Based on this type, the lib is capable to infer things like return types :
const result = await collection.lookupIn('book::01').get('authors');
// ^? string[]
const result = await collection
.mutateIn('book::01')
.arrayAppend(''); // Autocompletion offers `authors` because it is a mutable array
Considerations
I was wondering if everything should be pre-calculated and stored into the big type, or if on the contrary it should be evaluated at the function level.
I went for the evaluation at the function level. Nothing is pre-calculated, valid paths and return types are defined locally.
Problem
When using the lib in a project, the editor response time is high.
Questions
When you write a line of code, does IntelliSense re-evaluate all the types of the project ? Only the imported ones ? What if the imports have not been changed ?
Obviously reducing the amount of work done by TS is the key point.
How can I exploit the internal mechanisms to lower the response time ?
Resources
For the interested reader, here are the resources I’ve found to contain interesting data. I don’t provide extracts as they are not required to understand the question.
- https://github.com/microsoft/TypeScript/wiki/Performance
- https://github.com/microsoft/TypeScript/wiki/Performance-Tracing
- https://www.bajorunas.tech/blog/typescript-compiler-performance
- https://trpc.io/blog/typescript-performance-lessons