Skip to content

Add node:sqlite benchmark #1334

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions benchmark/drivers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ module.exports = new Map([
return db;
}],
]);

const moduleExists = (m) => { try { return require.resolve(m); } catch (e) {} };
if (moduleExists('node:sqlite')) {
module.exports.set('node:sqlite', async (filename, pragma) => {
const db = new (require('node:sqlite').DatabaseSync)(filename, {open: true});
for (const str of pragma) db.exec(`PRAGMA ${str}`);
return db;
});
}
2 changes: 1 addition & 1 deletion benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ for (const trial of trials) {
const ctx = createContext(trial, driver);
process.stdout.write(`${driver} (running...)\n`);
try {
const result = execFileSync('node', ['./benchmark.js', ctx], { stdio: 'pipe', encoding: 'utf8' });
const result = execFileSync('node', [...process.execArgv, './benchmark.js', ctx], { stdio: 'pipe', encoding: 'utf8' });
Copy link
Member

Choose a reason for hiding this comment

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

What's the purpose of ...process.execArgv?

Copy link
Author

Choose a reason for hiding this comment

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

If you don't pass --experimental-sqlite to node, the node:sqlite module will not be available. This makes it so node child processes get the same node-specific arguments as the parent.

console.log(erase() + `${driverName} x ${result}`);
} catch (err) {
console.log(erase() + clc.red(`${driverName} ERROR (probably out of memory)`));
Expand Down
11 changes: 11 additions & 0 deletions benchmark/types/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ exports['node-sqlite3'] = async (db, { table, columns }) => {
.map(([k, v]) => ({ ['@' + k]: v })));
return () => db.run(sql, row);
};

exports['node:sqlite'] = (db, { table, columns }) => {
const sql = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${columns.map(x => '@' + x).join(', ')})`;
const row = Object.assign({}, ...Object.entries(db.prepare(`SELECT * FROM ${table} LIMIT 1`).get())
.filter(([k]) => columns.includes(k))
.map(([k, v]) => ({ ['@' + k]: v })));
return () => {
const stmt = db.prepare(sql);
return stmt.run(row);
}
};
9 changes: 9 additions & 0 deletions benchmark/types/select-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ exports['node-sqlite3'] = async (db, { table, columns, count }) => {
let rowid = -100;
return () => db.all(sql, (rowid += 100) % count + 1);
};

exports['node:sqlite'] = (db, { table, columns, count }) => {
const sql = `SELECT ${columns.join(', ')} FROM ${table} WHERE rowid >= ? LIMIT 100`;
let rowid = -100;
return () => {
const stmt = db.prepare(sql);
Copy link
Member

Choose a reason for hiding this comment

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

The SQLITE_PREPARE_PERSISTENT flag is just an optimization hint. You should be able to reuse prepared statements without it.

return stmt.all((rowid += 100) % count + 1);
}
};
16 changes: 16 additions & 0 deletions benchmark/types/select-iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ exports['node-sqlite3'] = async (db, { table, columns, count }) => {
})();
};
};

exports['node:sqlite'] = (db, { table, columns, count }) => {
const sql = `SELECT ${columns.join(', ')} FROM ${table} WHERE rowid >= ? LIMIT 100`;
let rowid = -100;

if (!("iterate" in require("node:sqlite").StatementSync.prototype)) {
// Error: StatementSync.iterate is not a function (added in Node v23.4.0+)
return () => {};
}

return () => {
// Error: statement has been finalized
// for (const row of db.prepare(sql).iterate((rowid += 100) % count + 1)) {}
Copy link
Member

@JoshuaWise JoshuaWise Apr 28, 2025

Choose a reason for hiding this comment

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

I'll probably wait to merge this until node:sqlite is a little more stable. But thanks for putting this together!

Copy link
Author

Choose a reason for hiding this comment

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

When I made this there were a few issues with node:sqlite. You can try to rerun it today against latest node and see if they are fixed or adjust the code if the functions are different. I think it is still better than nothing so you can at least get an idea of how the two implementations perform, even if this test does not work.

return () => {};
};
};
9 changes: 9 additions & 0 deletions benchmark/types/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ exports['node-sqlite3'] = async (db, { table, columns, count }) => {
let rowid = -1;
return () => db.get(sql, ++rowid % count + 1);
};

exports['node:sqlite'] = (db, { table, columns, count }) => {
const sql = `SELECT ${columns.join(', ')} FROM ${table} WHERE rowid = ?`;
let rowid = -1;
return () => {
const stmt = db.prepare(sql);
return stmt.get(++rowid % count + 1);
}
};
13 changes: 13 additions & 0 deletions benchmark/types/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ exports['node-sqlite3'] = async (db, { table, columns, driver, pragma }) => {
}
});
};

exports['node:sqlite'] = (db, { table, columns }) => {
const sql = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${columns.map(x => '@' + x).join(', ')})`;
const row = Object.assign({}, ...Object.entries(db.prepare(`SELECT * FROM ${table} LIMIT 1`).get())
.filter(([k]) => columns.includes(k))
.map(([k, v]) => ({ ['@' + k]: v })));
return () => {
const stmt = db.prepare(sql);
db.exec(`BEGIN`);
for (let i = 0; i < 100; ++i) stmt.run(row);
db.exec(`COMMIT`);
}
};