0Pricing
Vibe Coding · 课时

性能与成本

构建快速应用,并合理控制人工智能和 API 费用。

性能与成本 是 CoddyKit 上的免费 Vibe Coding 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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!

常见问题解答

「性能与成本」课时是免费的吗?

是的 — 「性能与成本」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vibe Coding 课程的其余内容,请升级到 CoddyKit PRO。 Vibe Coding 课程共包含 4 节课。

「性能与成本」这节课中我会学到什么?

构建快速应用,并合理控制人工智能和 API 费用。 你通过在浏览器中直接运行的动手代码来练习 Vibe Coding,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Vibe Coding 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Vibe Coding 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「性能与成本」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Vibe Coding 课中编写并运行代码吗?

能。每节 Vibe Coding 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用人工智能添加测试
  2. 人工智能代码安全基础
  3. 保持代码可维护
  4. 性能与成本
← 返回 Vibe Coding