kauderk/youtube-browser-api

Query Types Performance

kauderk opened this issue · 0 comments

Excalidraw Link Excalidraw Link


Premise:

I need help optimizing the performance of my Mapped Types..

Case:

On 972cb8fa
TThe /query endpoint is designed to accept a schema and return the shape either flattened or as requested, handling optional, partial, or undefined cases.

Problem:

While editing, the performance of inferred types on the IDE is slow. It won't deter anyone from using the program, but we're accustomed to fast iterations, and even a little bit slower is too much. There are too many mapped types.

DX won't hurt anybody right?

Escape Patch

There is a tsAny: boolean option on every Query to bypass typechecking. With over 4000+ base queries and their combinations, it is important to recover performance wherever possible. One could enable this option when the fetch logic is complete.


This could be so much more simple, hell even one single Mapped type...

STEPS

FlattenAgainstSchema
Walks down the schema until it finds a non-object, while doing so
saves the last key onto a stack (string).
It will resolve a mapped type of {[nested.optional.properties.with.the.response]: value}

BrachOut
If isVerbose continue executing, else shrink the nested paths <- right-to-left

PathsToOutput
Takes a Record (aka Path), and Value>
Searches the LastKey recursively on the Path to reach the last property.name (aka Leaf)
Then acts on objects and arrays:
for objects -> {parent:{property: value}} | parent.property
for arrays -> {parent: {[index]: value}} | parent.index.property
For each, carries variables for the OriginalPath, LeafToValue, isVerboseOrFlatten
If it is an optional/NullCoalescing path, shift the uncertainty to the parent,
meaning grab the "?" and put it higher on the Path
If it isVerbose
infer the BranchPath and append the Leaf, else it's just the leaf.path

Assemble
Walks down each branch and creates nested objects by pulling keys from the branch
It will return a mapped type of {[branch.to.value]: ReconstructFromBranch<branch.to.value>}

Merge
It gets the union of values of the ReconstructedFromBranch Map type
then it intersect all branches into a single Tree/Schema