Skip to content

Performance Test

Code Scratcher edited this page Oct 11, 2025 · 21 revisions

Below is a complete performance test for looping NOTIFY -> LISTEN for 1 second.

import pgPromise from 'pg-promise';
import {PgListener} from 'pg-listener';

const pgp = pgPromise();

const db = pgp({
    user: 'db-user',
    database: 'db-name',
    // etc.
});

const ls = new PgListener({pgp, db});

const totalTests = 10; // number of attempts
let stop = false;
const allCounts: number[] = [];

(async function test() {
    let count = 0;
    const r = await ls.listen(['channel_1'], {
        onMessage(msg) {
            count++;
        }
    });
    for (let i = 0; i < totalTests; i++) {
        count = 0;
        stop = false;
        while (!stop) {
            const payload = '** With a payload of 50 letters being passed in **';
            await r.notify(['channel_1'], payload);
        }
        if (i) {
            allCounts.push(count); // we skip the first one
        }
    }
    await r.cancel();
})();

setInterval(() => {
    stop = true;
    if (!ls.connections.length) {
        const avg = allCounts.reduce((a, b) => a + b, 0) / allCounts.length;
        console.log('Average:', avg);
        process.exit(0);
    }
}, 1000);

Results:

PostgreSQL Without Payload With a 50-letter payload With a 500-letter payload
v12 ~14,000 ~13,400 ~7,400 - 10,200
v16 ~14,000 ~13,300 ~7,000 - 9,500
v18 ~14,700 ~14,000 ~6,500 - 10,500

i.e. once we start using a bigger payload, significant performance fluctuations start to show.

Test Environment:

  • NodeJS v24.10.0
  • Windows 11 Pro
  • PostgreSQL versions: 12, 16 and 18
  • pg-listener v1.0.0
  • pg-promise v12.2.0

Clone this wiki locally