marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
MIT License
839 stars 57 forks source link

perf(types): prevent immediate deep and circular resolution for deep mock #123

Closed millsp closed 1 year ago

millsp commented 1 year ago

Hey @marchaos :wave:, a user has opened this issue in Prisma, and after further investigation I pinned the performance issue to come from the MockDeep types provided here. We recently introduced meta types that are hidden under symbol. There's noting special about these types except that they can be very deep and recursive, but it's all objects/records in the end. I found that MockDeep caused/forced TypeScript to evaluate these types.

While this is not obvious, any intersection on a mapped type (eg. compute) itself will cause the mapped type to be resolved, otherwise TS would defer that for later (eg. when you access it). So MockDeep was forcing resolution on very huge and recursive types in the PrismaClient. This PR does not make things easier to read, but does make things more efficient, by forcing resolution within the mapped type itself. Also I had to re-intersect with the original type as well to preserve the full shape, including private props which mapped types remove.

With that, I was able to compile a huge PrismaClient like before we introduced this special type metadata on the symbol key.

marchaos commented 1 year ago

Hey, @millsp - thanks for the PR, looks good! Just checking it locally and will merge if all good.

marchaos commented 1 year ago

released in 3.0.5

millsp commented 1 year ago

Thanks!