Skip to content

Conversation

nathanwhit
Copy link
Member

@nathanwhit nathanwhit commented Jul 25, 2025

Benchmark (what drove this):

import postgres from "npm:postgres";
const sql = postgres();

// Create the table if it doesn't exist
await sql`
    CREATE TABLE IF NOT EXISTS "silly_postgres_bench" (
      id SERIAL PRIMARY KEY,
      first_name TEXT NOT NULL,
      last_name TEXT NOT NULL,
      email TEXT NOT NULL UNIQUE,
      dob TEXT NOT NULL
    )
  `;

// Check if users already exist
const existingUsers =
  await sql`SELECT COUNT(*) as count FROM "silly_postgres_bench"`;

if (+(existingUsers?.[0]?.count ?? existingUsers?.count) < 100) {
  // Generate 100 users if none exist
  const users = Array.from({ length: 100 }, (_, i) => ({
    first_name: `FirstName${i}`,
    last_name: `LastName${i}`,
    email: `user${i}@example.com`,
    dob: new Date(
      1970 + Math.floor(Math.random() * 30),
      Math.floor(Math.random() * 12),
      Math.floor(Math.random() * 28),
    )
      .toISOString()
      .split("T")[0],
  }));

  // Insert all users
  await sql`
      INSERT INTO silly_postgres_bench ${sql(users)}
    `;
}

console.time("deno");
const promises = [];
for (let i = 0; i < 100_000; i++) {
  promises.push(sql`SELECT * FROM "silly_postgres_bench" LIMIT 100`);
  if (i % 100 === 0 && promises.length > 1) {
    await Promise.all(promises);
    promises.length = 0;
  }
}
await Promise.all(promises);
console.timeEnd("deno");

Results:

main:

deno: 11981ms

This PR:

deno: 5720ms

@nathanwhit nathanwhit force-pushed the buffer-tostring-utf8-opt branch from 5082d9f to c5e5060 Compare July 29, 2025 22:43
Copy link
Member

@littledivy littledivy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nathanwhit nathanwhit merged commit 2a3605b into denoland:main Jul 30, 2025
18 checks passed
@nathanwhit nathanwhit deleted the buffer-tostring-utf8-opt branch July 30, 2025 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants