Skip to content

Commit 5ca00dc

Browse files
Add node:sqlite benchmark
Co-authored-by: weary-adventurer <30081929+weary-adventurer@users.noreply.github.com>
1 parent a447a47 commit 5ca00dc

File tree

7 files changed

+68
-1
lines changed

7 files changed

+68
-1
lines changed

benchmark/drivers.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ module.exports = new Map([
1919
return db;
2020
}],
2121
]);
22+
23+
const moduleExists = (m) => { try { return require.resolve(m); } catch (e) {} };
24+
if (moduleExists('node:sqlite')) {
25+
module.exports.set('node:sqlite', async (filename, pragma) => {
26+
const db = new (require('node:sqlite').DatabaseSync)(filename, {open: true});
27+
for (const str of pragma) db.exec(`PRAGMA ${str}`);
28+
return db;
29+
});
30+
}

benchmark/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ for (const trial of trials) {
6969
const ctx = createContext(trial, driver);
7070
process.stdout.write(`${driver} (running...)\n`);
7171
try {
72-
const result = execFileSync('node', ['./benchmark.js', ctx], { stdio: 'pipe', encoding: 'utf8' });
72+
const result = execFileSync('node', [...process.execArgv, './benchmark.js', ctx], { stdio: 'pipe', encoding: 'utf8' });
7373
console.log(erase() + `${driverName} x ${result}`);
7474
} catch (err) {
7575
console.log(erase() + clc.red(`${driverName} ERROR (probably out of memory)`));

benchmark/types/insert.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@ exports['node-sqlite3'] = async (db, { table, columns }) => {
1414
.map(([k, v]) => ({ ['@' + k]: v })));
1515
return () => db.run(sql, row);
1616
};
17+
18+
exports['node:sqlite'] = (db, { table, columns }) => {
19+
const sql = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${columns.map(x => '@' + x).join(', ')})`;
20+
const row = Object.assign({}, ...Object.entries(db.prepare(`SELECT * FROM ${table} LIMIT 1`).get())
21+
.filter(([k]) => columns.includes(k))
22+
.map(([k, v]) => ({ ['@' + k]: v })));
23+
return () => {
24+
const stmt = db.prepare(sql);
25+
return stmt.run(row);
26+
}
27+
};

benchmark/types/select-all.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ exports['node-sqlite3'] = async (db, { table, columns, count }) => {
1212
let rowid = -100;
1313
return () => db.all(sql, (rowid += 100) % count + 1);
1414
};
15+
16+
exports['node:sqlite'] = (db, { table, columns, count }) => {
17+
const sql = `SELECT ${columns.join(', ')} FROM ${table} WHERE rowid >= ? LIMIT 100`;
18+
let rowid = -100;
19+
return () => {
20+
const stmt = db.prepare(sql);
21+
return stmt.all((rowid += 100) % count + 1);
22+
}
23+
};

benchmark/types/select-iterate.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,19 @@ exports['node-sqlite3'] = async (db, { table, columns, count }) => {
2121
})();
2222
};
2323
};
24+
25+
exports['node:sqlite'] = (db, { table, columns, count }) => {
26+
const sql = `SELECT ${columns.join(', ')} FROM ${table} WHERE rowid >= ? LIMIT 100`;
27+
let rowid = -100;
28+
29+
if (!("iterate" in require("node:sqlite").StatementSync.prototype)) {
30+
// Error: StatementSync.iterate is not a function (added in Node v23.4.0+)
31+
return () => {};
32+
}
33+
34+
return () => {
35+
// Error: statement has been finalized
36+
// for (const row of db.prepare(sql).iterate((rowid += 100) % count + 1)) {}
37+
return () => {};
38+
};
39+
};

benchmark/types/select.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ exports['node-sqlite3'] = async (db, { table, columns, count }) => {
1212
let rowid = -1;
1313
return () => db.get(sql, ++rowid % count + 1);
1414
};
15+
16+
exports['node:sqlite'] = (db, { table, columns, count }) => {
17+
const sql = `SELECT ${columns.join(', ')} FROM ${table} WHERE rowid = ?`;
18+
let rowid = -1;
19+
return () => {
20+
const stmt = db.prepare(sql);
21+
return stmt.get(++rowid % count + 1);
22+
}
23+
};

benchmark/types/transaction.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,16 @@ exports['node-sqlite3'] = async (db, { table, columns, driver, pragma }) => {
3838
}
3939
});
4040
};
41+
42+
exports['node:sqlite'] = (db, { table, columns }) => {
43+
const sql = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${columns.map(x => '@' + x).join(', ')})`;
44+
const row = Object.assign({}, ...Object.entries(db.prepare(`SELECT * FROM ${table} LIMIT 1`).get())
45+
.filter(([k]) => columns.includes(k))
46+
.map(([k, v]) => ({ ['@' + k]: v })));
47+
return () => {
48+
const stmt = db.prepare(sql);
49+
db.exec(`BEGIN`);
50+
for (let i = 0; i < 100; ++i) stmt.run(row);
51+
db.exec(`COMMIT`);
52+
}
53+
};

0 commit comments

Comments
 (0)