Description
Current behavior
Rows returned by get(), all() and iterate() are created with a null prototype. The V8 API that does this produces dictionary-mode objects, and V8 will not cache a prototype transition on a dictionary map, so every row also gets its own freshly allocated hidden class. Rows from the same statement therefore share no shape: they are slow to build, and every property access on them in user code is megamorphic.
Proposal
Drop the null prototype and build rows with v8::DictionaryTemplate, cached per statement. Rows become ordinary objects, and every row of a statement shares one hidden class.
Pros
all()is 10–37% faster depending on the query; reading the rows afterwards is far cheaper still.- Consistent with
run(), which already returns an ordinary object, and withbetter-sqlite3.
Cons
- Semver-major.
- Rows can no longer be indexed by untrusted keys without
Object.hasOwn()—row.toStringandrow.constructorstart resolving throughObject.prototype. - User code comparing rows against
{ __proto__: null, ... }breaks.
Prototype pollution is not a concern either way: rows are built by defining own properties directly, so a __proto__ column is an own property and never reaches Object.prototype.
I have benchmarks and a working implementation if there is interest.
Activity
bakkot commentedon Sep 4, 2026
I would be very concerned about it becoming unsafe to index rows with untrusted keys. Is there any way to get performant objects with a null prototype? Possibly with a new V8 API, if that's what it would take?
aduh95 commentedon Sep 5, 2026
I don't really understand this statement, and more specifically how the part after the colon is related to the part before it. Whether prototype pollution is a concern will depend on the use-case – it's not always possible to know if the prototype has been tampered with, many folks are happy to simply assume it's not a thing, but whether you like it or not, inheriting from
Object.prototypeincreases the attack surface which, as long you use code you haven't written/reviewed (e.g. from npm), is relatively trivial to exploit.Renegade334 commentedon Sep 5, 2026
V8 refusing to use an existing map for a null-prototype object is somewhat surprising.
Does the same apply for an object with a common prototype that is itself null-prototyped? (eg.
{...} > SQLResultRow.prototype > null)araujogui commentedon Sep 5, 2026
Yes, I went and had a look and it seems like a small V8 change. Given the points raised here, I'd rather go that way than drop the null prototype.
The reason it's slow today is that
v8::Object::New(isolate, Null(isolate), names, values, n)always builds a dictionary-mode object, and V8 won't cache a prototype transition when the source map is a dictionary map, so every row ends up allocating a freshMap. Rows from the same statement share no shape at all, which is why they're slow to build and slow to read.v8::DictionaryTemplateis the API that would fix that, since it caches one map per(context, template), but it hardcodesObject.prototypein three places inDictionaryTemplateInfo::NewInstance, including a release-modeCHECK_EQ. The good news is that prototype transitions are cached when the source map is fast, and the object-literal map it roots at is fast, so the transition only has to happen once per shape and the existing cache takes it from there. I tried it locally and it came out to 8 lines:With that, rows come out with a null prototype, in fast-properties mode, and all sharing one map, at the same per-row cost as the
Object.prototypeversion. So the null prototype is basically free once the shape is cached.Tbh I'm not very familiar with V8 internals, so I may well be missing something here.
sqlite: use shared-shape objects for result rows
sqlite: use shared-shape objects for result rows
sqlite: use shared-shape objects for result rows