Performance: MySQL connection with mysql2
MEDIUM IMPACT
This affects the initial page or API response load time by how quickly the database connection is established and queries are executed.
import mysql from 'mysql2/promise'; const pool = mysql.createPool({host: 'localhost', user: 'root', database: 'test', waitForConnections: true, connectionLimit: 10}); async function getUsers() { const [rows] = await pool.query('SELECT * FROM users'); return rows; }
const mysql = require('mysql2'); const connection = mysql.createConnection({host: 'localhost', user: 'root', database: 'test'}); connection.query('SELECT * FROM users', (err, results) => { if (err) throw err; console.log(results); }); connection.end();
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Single connection per query | N/A | N/A | N/A | [X] Bad |
| Connection pool with async queries | N/A | N/A | N/A | [OK] Good |