|
1 | | -const sqlite3 = require('sqlite3'); |
2 | | -const Sqlite = require('../'); |
3 | | - |
4 | | -const t = process.hrtime(); |
5 | | -const db = new sqlite3.Database(':memory:'); |
6 | | -db.serialize(() => { |
7 | | - db.run('CREATE TABLE lorem (info TEXT)'); |
8 | | - |
9 | | - const stmt = db.prepare('INSERT INTO lorem VALUES (?)'); |
10 | | - for (let i = 0; i < 10; i++) { |
11 | | - stmt.run(`Ipsum ${i}`); |
12 | | - } |
13 | | - stmt.finalize(); |
14 | | - |
15 | | - db.each('SELECT rowid AS id, info FROM lorem', (err, row) => { |
16 | | - // console.log(`${row.id}: ${row.info}`); |
17 | | - }); |
18 | | - db.close(); |
19 | | - console.log('sqlite3', process.hrtime(t)[1]); |
20 | | -}); |
21 | | - |
22 | | -const connector = new Sqlite.Sqlite(); |
23 | | - |
24 | | -(async () => { |
25 | | - const s = process.hrtime(); |
26 | | - // Creating a regular async connection |
27 | | - const conn = await connector.open({ |
28 | | - verbose: true // process.env.NODE_ENV !== 'production' |
29 | | - }); |
30 | | - // conn.prepare('CREATE TABLE lorem (info TEXT)'); |
31 | | - // conn.close(); |
32 | | - console.log('neon sqlite', process.hrtime(s)[1]); |
33 | | -})(); |
34 | | - |
35 | | -(async () => { |
36 | | - const s = process.hrtime(); |
37 | | - // Creating a regular async connection |
38 | | - // Sqlite.example(); |
39 | | - // conn.close(); |
40 | | - console.log('example', process.hrtime(s)[1]); |
41 | | -})(); |
| 1 | +/* eslint import/no-extraneous-dependencies: off */ |
| 2 | +const Benchmark = require('benchmark'); |
| 3 | +const { Database } = require('sqlite3'); |
| 4 | +const betterSqlite = require('better-sqlite3'); |
| 5 | +const { Sqlite } = require('..'); |
| 6 | + |
| 7 | +const suite = new Benchmark.Suite(); |
| 8 | + |
| 9 | +const defaultOpts = { |
| 10 | + maxTime: 0, |
| 11 | + minSamples: 10 |
| 12 | +}; |
| 13 | + |
| 14 | +suite |
| 15 | + .add('sqlite3', { |
| 16 | + fn: () => { |
| 17 | + const db = new Database(':memory:'); |
| 18 | + |
| 19 | + db.serialize(() => { |
| 20 | + db.run('CREATE TABLE lorem (info TEXT)'); |
| 21 | + |
| 22 | + const stmt = db.prepare('INSERT INTO lorem VALUES (?)'); |
| 23 | + for (let i = 0; i < 10; i++) { |
| 24 | + stmt.run(`Ipsum ${i}`); |
| 25 | + } |
| 26 | + stmt.finalize(); |
| 27 | + |
| 28 | + db.close(); |
| 29 | + }); |
| 30 | + }, |
| 31 | + ...defaultOpts |
| 32 | + }) |
| 33 | + .add('better-sqlite3', { |
| 34 | + fn: () => { |
| 35 | + const db = betterSqlite(':memory:'); |
| 36 | + |
| 37 | + db.exec('CREATE TABLE lorem (info TEXT)'); |
| 38 | + |
| 39 | + const stmt = db.prepare('INSERT INTO lorem VALUES (?)'); |
| 40 | + for (let i = 0; i < 10; i++) { |
| 41 | + stmt.run(`Ipsum ${i}`); |
| 42 | + } |
| 43 | + |
| 44 | + db.close(); |
| 45 | + }, |
| 46 | + ...defaultOpts |
| 47 | + }) |
| 48 | + .add('sqlite/sqlite', { |
| 49 | + fn: () => { |
| 50 | + const conn = new Sqlite(); |
| 51 | + conn.open({ |
| 52 | + verbose: true, |
| 53 | + database: ':memory:' |
| 54 | + }); |
| 55 | + conn.execute('DROP TABLE IF EXISTS contacts;'); |
| 56 | + conn.close(); |
| 57 | + }, |
| 58 | + ...defaultOpts |
| 59 | + }) |
| 60 | + .on('complete', function complete() { |
| 61 | + if (suite.aborted) return; |
| 62 | + console.log(`Fastest is ${this.filter('fastest').map('name')}`); |
| 63 | + console.log('Higher is better:'); |
| 64 | + suite |
| 65 | + .map(benchmark => ({ name: benchmark.name, score: benchmark.hz })) |
| 66 | + .forEach(benchmark => { |
| 67 | + console.log(benchmark.name, benchmark.score); |
| 68 | + }); |
| 69 | + }) |
| 70 | + .run(); |
0 commit comments