Skip to content

Instantly share code, notes, and snippets.

@scf4
Created August 6, 2017 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scf4/10af79a95d17aa9692a3e63e2baf0e1a to your computer and use it in GitHub Desktop.
Save scf4/10af79a95d17aa9692a3e63e2baf0e1a to your computer and use it in GitHub Desktop.
const { types, Client, Pool } = require('pg');
const Knex = require('knex');
const knex = new Knex({
client: 'pg',
connection: 'postgresql://localhost/postgres',
});
const benchmark = async (fn, num) => {
const start = new Date().getTime();
await Promise.all(Array(num).fill(fn()));
const ms = new Date().getTime() - start;
console.log(`${num} took ${ms}ms`);
};
(async () => {
await knex.transaction(async trx => {
const fn = () => trx.raw('select 1');
await benchmark(fn, 1000);
});
const pool = new Pool({
database: 'postgres',
});
const client = await pool.connect();
const fn = () => client.query('select 1');
await benchmark(fn, 1000);
})();
@google-master
Copy link

try to see result of select random() instead of select 1, re-write this code and you'll find that result of both benchmarks are almost the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment