Skip to content

speed up assigning Transaction to a Kysely - #1962

Open
koskimas wants to merge 2 commits into
masterfrom
speed-up-transaction-type-checks
Open

speed up assigning Transaction to a Kysely#1962
koskimas wants to merge 2 commits into
masterfrom
speed-up-transaction-type-checks

Conversation

@koskimas

Copy link
Copy Markdown
Member

It's a very common thing to have a function that takes a Kysely as an argument and then to pass in a Transaction instance.

Before this commit it was extremely slow. Like hundreds of thousands of type instantations slow, even for a trivial database.

Case TS Instantiations (before → after) Check time (before → after)
Kysely assignable to Kysely 5.9 11,713 → 11,731 60ms → 60ms
6.0 11,786 → 11,804 60ms → 60ms
7.0 56,344 → 56,362 25ms → 25ms
Transaction assignable to Kysely 5.9 149,117 → 11,779 270ms → 60ms
6.0 162,354 → 11,852 280ms → 60ms
7.0 290,374 → 56,410 118ms → 24ms
ControlledTransaction assignable to Kysely 5.9 149,342 → 11,793 270ms → 60ms
6.0 162,579 → 11,866 290ms → 60ms
7.0 291,279 → 56,424 118ms → 25ms
ControlledTransaction assignable to Transaction 5.9 149,354 → 316,077 260ms → 480ms
6.0 162,591 → 330,118 290ms → 500ms
7.0 291,315 → 338,539 123ms → 173ms

This change is debatable. It doesn't actually remove the infinite recursion (TS7 still produces more instantations) and it makes assigning ControlledTransaction to Transaction slower.

I'm fine with not merging this. Let's first see if it's possible to get rid of the recursion alltogether.

@koskimas
koskimas requested a review from igalklebanov July 31, 2026 04:31
@koskimas koskimas added enhancement New feature or request typescript Related to Typescript performance issue Something is slow af labels Jul 31, 2026
@vercel

vercel Bot commented Jul 31, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
kysely Ready Ready Preview Aug 3, 2026 11:12am

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Jul 31, 2026

Copy link
Copy Markdown

commit: 808047a

@github-actions

Copy link
Copy Markdown

⏱️ TypeScript Benchmark Results

✅ ⏱️  No benchmark changes detected.

It's a very common thing to have a function that takes a Kysely as
an argument and then to pass in a Transaction instance.

Before this commit it was extremely slow. Like hundreds of thousands
of type instantations slow, even for a trivial database.
@koskimas
koskimas force-pushed the speed-up-transaction-type-checks branch from 2dcf72e to 36622b0 Compare July 31, 2026 04:35
@koskimas koskimas changed the title speed up assigning transaction to a kysely speed up assigning Transaction to a Kysely Jul 31, 2026
Comment thread src/kysely.ts
* This is an intersection of {@link TransactionMethods} and {@link Kysely}
* rather than a subclass of `Kysely` for type check performance reasons.
*
* `Transaction` is still a class at runtime, so `db instanceof Transaction`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not encourage instanceof.

Comment thread src/kysely.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request performance issue Something is slow af typescript Related to Typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants