google / schema-dts

JSON-LD TypeScript types for Schema.org vocabulary
Apache License 2.0
889 stars 33 forks source link

Name intermediate Leaf types #99

Closed Eyas closed 4 years ago

Eyas commented 4 years ago

Rather than inlining a "leaf" type as an intersection in a big union, name it. This should hopefully help with #34.

This is based on advice from @amcasey regarding compiler performance:

I've definitely seen some impressive perf improvements from naming intermediate types (because it can short-circuit structural type comparison, which is very expensive).

This fits neatly with my initial description of the Schema.org type system here: https://blog.eyas.sh/2019/05/modeling-schema-org-schema-with-typescript-the-power-and-limitations-of-the-typescript-type-system/

There's still some room for improvements:

Also in this PR,

amcasey commented 4 years ago

@Eyas Cool! What sort of perf gain did you see?

For the benefit of future readers, I should clarify that when naming intermediate types helps (which is rare), it can help a lot.

Eyas commented 4 years ago

It makes a bit of a (positive) difference in 3.7 and 3.8, but might have regressed in 3.9.1. 95% confidence interval is pretty wide though.

TS 3.7:

TS 3.8:

TS 3.9.1-rc:

Eyas commented 4 years ago

This is using --extendedDiagnostics and looking at "Memory used". The repro case is simple:

import { WithContext, Article } from "schema-dts";

export const article: WithContext<Article> = {
    "@context": "https://schema.org",
    "@type": "Article",
    name: [
        "Woo", "Hoo"
    ]
};

with either schema-dts version installed. Node 13.

amcasey commented 4 years ago

Thanks! One day, I'll figure out why node memory usage is so variable. :wink: