Ticker

6/recent/ticker-posts

⚡ Performance & Timing APIs in JavaScript – Stop Guessing, Start Measuring

⚡ Performance & Timing APIs in JavaScript – Stop Guessing, Start Measuring

Ever said:

👉 “This feels slow…”
👉 “Maybe this API is taking time?”
👉 “UI lag ah iruku… but not sure why”

That’s the problem.

Most devs guess performance issues instead of measuring them.

JavaScript actually gives you solid tools to measure exactly what’s happening — not vibes, not assumptions.

Let’s talk about the Performance & Timing APIs the way you’ll actually use them.

⚡ The Core Idea

You don’t optimize blindly.

👉 You measure → understand → optimize

And this is where the performance API comes in.

🔎 performance.now() – The Simple One You’ll Actually Use

const start = performance.now();
// some expensive operation
for (let i = 0; i < 1000000; i++) {}
const end = performance.now();
console.log(`Took ${end - start} ms`);

If you just want to check how long something takes:

const start = performance.now();
// some expensive operation

Why not Date.now()?

👉 performance.now() is:

  • More precise (microseconds)
  • Not affected by system clock changes

This is your go-to for quick profiling.

🔥 Real Use Case: Measure API Call Time

async function fetchData() {
  const start = performance.now();
  const apiUrl = "https://jsonplaceholder.typicode.com/posts";

  const res = await fetch();
  const data = await res.json();

  const end = performance.now();

  console.log(`API took ${end - start} ms`);
  return data;
}

Simple… but super useful when debugging slow APIs.

🧠 performance.mark() & performance.measure()

Now this is where things get a bit more structured.

Instead of manual start/end variables:

performance.mark("start-task");

// task
for (let i = 0; i < 1000000; i++) {}

performance.mark("end-task");

performance.measure("task-duration", "start-task", "end-task");

console.log(performance.getEntriesByName("task-duration"));

👉 Useful when:

  • You want named measurements
  • You’re tracking multiple steps

📊 Navigation Timing (Page Load Insights)

You can also inspect how your page loads:

const timing = performance.getEntriesByType("navigation")[0];

console.log("DOM Load:", timing.domContentLoadedEventEnd);
console.log("Page Load:", timing.loadEventEnd);

This tells you:

  • When DOM was ready
  • When everything fully loaded

👉 Helps identify slow initial loads.

💡 Real Developer Insight

Here’s the thing most people miss:

👉 Measuring is easy. Understanding is hard.

You might see:

  • API → 300ms
  • Render → 50ms

But users still feel it’s slow.

Why?

👉 Because perceived performance ≠ actual numbers

Things like:

  • blocking main thread
  • large JS bundles
  • layout reflows

…don’t always show clearly in simple logs.

That’s when you move to:

👉 Chrome DevTools → Performance tab

⚠️ Where Devs Usually Mess Up

1️⃣ Measuring once and assuming it's final

Performance varies based on:

  • network
  • device
  • load

2️⃣ Ignoring async timing issues

performance.now();
// async call

You might measure wrong segments.

3️⃣ Over-optimizing small things

Saving 5ms doesn’t matter if your API takes 2 seconds.

4️⃣ Not using DevTools

Console logs ≠ full picture.

🚀 Best Practice Summary

✅ Use performance.now() for quick and precise timing
✅ Use performance.mark() for structured measurements
✅ Measure real user flows, not isolated code
✅ Focus on biggest bottlenecks first
✅ Combine API data with DevTools analysis

Reactions

Post a Comment

0 Comments