Vibe Coding · บทเรียน

ประสิทธิภาพและต้นทุน

แอปที่รวดเร็วและค่าใช้จ่ายปัญญาประดิษฐ์/API ที่สมเหตุสมผล

บทเรียน 4 จาก 413 ขั้นตอน

ประสิทธิภาพและต้นทุน เป็นบทเรียน Vibe Coding ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vibe Coding และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Fast Apps, Sane Bills

Your app works — but is it fast, and what does it cost to run? Vibe-coded apps often have hidden performance problems and surprise AI/API bills. This lesson shows you how to keep both under control.

You don't need deep optimization skills. You need a few habits and the right questions to ask your AI.

Measure Before You Optimize

The biggest mistake is guessing where the slowness is. Measure first. A quick timer around suspicious code tells you the truth — often the bottleneck isn't where you'd expect.

This snippet times how long a piece of work takes.

function slowWork() {
  let total = 0;
  for (let i = 0; i < 1000000; i++) total += i;
  return total;
}

const start = performance.now();
slowWork();
const ms = performance.now() - start;
console.log('Took ' + ms.toFixed(2) + ' ms');

The Most Common Slowdown: Doing Work in a Loop

A classic performance killer is calling a database or API inside a loop — one call per item, hundreds of times. The fix is usually to fetch everything in one go.

Spot the difference.

// SLOW: one database call per user (the N+1 problem)
for (const id of userIds) {
  const user = await db.getUser(id);
  process(user);
}

// FAST: one call for all of them
const users = await db.getUsers(userIds);
users.forEach(process);

Ask AI to Find Bottlenecks

You can hand your code to AI and ask it to hunt for the slow parts. Be specific about what you suspect so the answer is focused.

Review this code for performance problems. Look especially for:
- database or API calls inside loops
- the same data fetched repeatedly
- work that could be cached
Rank the issues by impact and show the fix for the worst one. Here is the code: <paste>

Cache What Doesn't Change

If you compute or fetch the same thing over and over, save the result and reuse it. This is caching. A simple in-memory cache can turn a slow repeated call into an instant lookup.

const cache = new Map();

function getExchangeRate(currency) {
  if (cache.has(currency)) return cache.get(currency); // instant
  const rate = expensiveLookup(currency);             // slow, runs once
  cache.set(currency, rate);
  return rate;
}

function expensiveLookup(c) { return c === 'EUR' ? 1.08 : 1; }
console.log(getExchangeRate('EUR'), getExchangeRate('EUR'));

Don't Ship a Giant Frontend

On the web, performance is often about how much you send to the browser. Huge images and bloated JavaScript bundles make pages load slowly, especially on phones.

Ask your AI tool to slim things down — it knows the modern tricks.

My web page loads slowly. Help me reduce what's sent to the browser:
- compress and lazy-load images
- split the JavaScript so unused code isn't loaded upfront
- point out any large dependencies I could drop
Tell me the single change with the biggest impact first.

AI Calls Cost Real Money

If your app calls an AI model (like the Claude or OpenAI API), every request costs money based on tokens — roughly, the amount of text in and out. A popular app making many calls can run up a real bill fast.

The instinct to send huge prompts "just in case" is what blows budgets. Send only what's needed.

Cut AI Costs Without Cutting Quality

Several levers reduce AI spend dramatically:

  • Use a smaller/cheaper model for simple tasks; save the powerful one for hard ones.
  • Shorten prompts — trim filler and only include relevant context.
  • Cache answers for repeated questions instead of re-asking the model.
  • Limit the output length when you don't need a long reply.
My app calls an AI API on every user message and the bill is high. Help me cut cost:
- which requests could use a cheaper, smaller model?
- how do I trim the prompt without losing accuracy?
- can I cache repeated questions?
- how do I cap the response length safely?

Set Budgets and Alerts

The simplest way to avoid a shocking bill is to set a spending limit and an alert. Most API dashboards (Anthropic, OpenAI, your hosting provider) let you cap usage or get notified at a threshold.

Turn this on before you launch, not after the bill arrives.

Don't Over-Optimize Too Early

Balance matters. Spending days shaving milliseconds off an app with ten users is wasted effort. The rule: make it work, make it correct, then make it fast — and only where it actually matters.

Measure, fix the biggest real problem, and move on. Premature optimization makes code complex for no benefit.

A Performance & Cost Checklist

Before you ship, run through this with your AI:

  • Measured the slow parts — not guessed.
  • No database/API calls stuck inside loops.
  • Repeated expensive work is cached.
  • Frontend ships compressed images and split JS.
  • AI calls use the right-sized model and trimmed prompts.
  • Spending limits and alerts are set.

Quick Check

Your app feels slow. Before changing any code, what's the smartest first step?

Recap: Fast and Affordable

You learned to keep vibe-coded apps fast and cheap: measure before optimizing, avoid calls inside loops, cache repeated work, slim down the frontend, and control AI/API costs with smaller models, trimmed prompts, caching, and spending alerts.

And don't over-optimize — fix what matters, where it matters. That wraps up Productionizing AI Code: your apps are now tested, secure, maintainable, fast, and affordable. Real builder territory!

เริ่มต้นได้ฟรี

เรียนรู้ JavaScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
25
บทเรียน
100

คำถามที่พบบ่อย

บทเรียน “ประสิทธิภาพและต้นทุน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ประสิทธิภาพและต้นทุน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vibe Coding ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ประสิทธิภาพและต้นทุน”

แอปที่รวดเร็วและค่าใช้จ่ายปัญญาประดิษฐ์/API ที่สมเหตุสมผล คุณปฏิบัติ Vibe Coding ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vibe Coding หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vibe Coding บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ประสิทธิภาพและต้นทุน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Vibe Coding นี้ได้ไหม

ได้ บทเรียน Vibe Coding ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเพิ่มการทดสอบด้วยปัญญาประดิษฐ์
  2. พื้นฐานความปลอดภัยสำหรับโค้ดปัญญาประดิษฐ์
  3. การรักษาโค้ดให้ดูแลต่อได้
  4. ประสิทธิภาพและต้นทุน
← กลับไปที่ Vibe Coding