Performance: PostgreSQL with Deno
This affects server response time and data fetching speed, impacting how fast the frontend receives data to render.
import { Client } from "https://deno.land/x/postgres/mod.ts"; const client = new Client(); await client.connect(); const ids = Array.from({ length: 100 }, (_, i) => i + 1); const queries = ids.map(id => client.queryArray("SELECT * FROM users WHERE id = $1", id)); const results = await Promise.all(queries); results.forEach(result => console.log(result.rows)); await client.end();
import { Client } from "https://deno.land/x/postgres/mod.ts"; const client = new Client(); await client.connect(); for (let i = 0; i < 100; i++) { const result = await client.queryArray("SELECT * FROM users WHERE id = $1", i + 1); console.log(result.rows); } await client.end();
| Pattern | Server Processing | Network Delay | Browser Impact | Verdict |
|---|---|---|---|---|
| Sequential queries on single connection | High CPU wait, blocking | Longer wait for data | Delays LCP | [X] Bad |
| Parallel queries with single connection | Reduced wait time | Faster data transfer | Improves LCP | [!] OK |
| New connection per request | High overhead per request | Slower response | Delays LCP | [X] Bad |
| Connection pooling reused | Low overhead | Fast response | Best LCP | [OK] Good |
Client from the postgres module in Deno?Client in postgres moduleClient is designed to establish a connection to a PostgreSQL database and execute SQL queries.Client in the postgres module.import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
const client = new Client({ user: "user", database: "testdb" });
await client.connect();
const result = await client.queryObject("SELECT 1 AS number");
await client.end();
console.log(result.rows[0].number);import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
const client = new Client({ user: "user", database: "testdb" });
const result = await client.queryObject("SELECT NOW()");
await client.end();
console.log(result.rows);await client.connect() before running the query.Pass the test and this pattern moves unknown