-
Notifications
You must be signed in to change notification settings - Fork 239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why freeze? #20
Comments
As you know, const qb = db
.selectFrom(['animal', 'person as p'])
.select('animal.name')
.distinctOn('p.firstName')
.whereRef('p.lastName', '=', 'animal.name')
.where('animal.name', 'in', ['foo', 'bar', 'baz'])
.whereExists((qb) =>
qb.subQuery('movie as m').whereRef('m.id', '=', 'p.id').selectAll()
) Dropping By using |
Actually, I just ran that test again and it takes around 0.01 milliseconds with and without freeze. I ran this on a M1 macbook air and node 16.12.0. Here's the test if you want to run it yourself: function test() {
const qb = ctx.db
.selectFrom(['person as p', 'pet'])
.select('pet.name')
.distinctOn('p.first_name')
.whereRef('p.last_name', '=', 'pet.name')
.where('pet.name', 'in', ['foo', 'bar', 'baz'])
.whereExists((qb) =>
qb.subQuery('movie as m').whereRef('m.id', '=', 'p.id').selectAll()
)
const result = qb.compile()
}
for (let i = 0; i < 1000; ++i) {
test()
}
const N = 100000
const t0 = new Date()
for (let i = 0; i < N; ++i) {
test()
}
const t1 = new Date()
console.log(`${(t1.getTime() - t0.getTime()) / N} ms`) |
OK, thank you! |
I still think it's unnecessary with read-only types, but it's probably harmless, except for little DX overhead. I can't imagine a case when freezing would help. Is there a possibility kysely objects are mutated by some other library? I am just curious. |
A good example is third party plugin or dialect that does something like this const something = somethingImmutable as any
something.someReadonlyField = 42
return something and then uses that somewhere else to get something done. Then people using that plugin or dialect would open issues here about some obscure bugs that plugin is causing. Also in some rare places Kysely needs to use the In these places where some things are declared as Since Also in my experience, libraries need to be as "fool proof" as possible. If there is a way to use the API the wrong way, people will find it and hack the hell out of it and then go to stackoverflow/reddit to scream how **king **it that library is when it's bugging because of the hacks. |
Thank you for such a detailed answer. I have no other question. As for "no clean way to prevent that", there is a lib implementing newtype pattern, https://github.com/gcanti/newtype-ts, I was using it, but the ceremony with that was so annoying I switched back to branded types, which can be violated, but that's the responsibility of the developer. Speaking of branded types, I am using io-ts a lot, so instead of a string or a number, I am using String64 or PositiveInteger etc. It's invaluable for handling untyped values from REST APIs etc. Just for fun, that how I was trapped by fp-ts. io-ts decode returns Either, and I was furious I can not extract a value "easily" so I had to learn pipe Either map, chain, match etc. 😂 |
I am just curious, why freeze? Isn't TypeScript readonly enough? I remember some issues related to freeze automerge/automerge#177
The text was updated successfully, but these errors were encountered: