All posts

How to Fix Performance Api Error in React

Learn how to diagnose and fix the performance api error in React. Includes code examples and prevention tips.

If you've run into a performance api error in your React project, you're not alone. This is one of the most common issues developers face, and fortunately the fix is usually straightforward once you understand the root cause.

Why This Happens

The Performance API error appears when your React code calls performance.mark(), performance.measure(), or similar methods in an environment where the performance global doesn't exist. This commonly happens during server-side rendering where browser APIs are unavailable.

The Solution

// Wrong: calling performance API during SSR
const mark = performance.mark("render-start");

// Fixed: guard with typeof check and useEffect
import {{ useEffect }} from "react";

function App() {{
  useEffect(() => {{
    // Safe: only runs in browser
    performance.mark("render-start");
    return () => {{
      performance.measure("render-time", "render-start");
    }};
  }}, []);
  return <div>My App</div>;
}}

Guard all performance calls with lifecycle hooks or typeof checks to handle SSR. Using useEffect ensures the code only runs client-side.

Additional Considerations

  • The Performance API is browser-only — Node.js provides a separate perf_hooks module with a different import pattern
  • Some older browsers have incomplete implementations of the Performance Timeline API
  • Always wrap performance instrumentation in try-catch blocks so measurement failures never break your application
  • Consider using the PerformanceObserver API for more robust measurement collection

Environment Detection Pattern

When writing isomorphic React code, create a utility function that safely wraps Performance API calls:

const safeMark = (name) => {
  if (typeof performance !== "undefined" && performance.mark) {
    performance.mark(name);
  }
};

[Bugsly](https://bugsly.dev) can track Performance API errors across your React deployment and help you identify exactly which environments and code paths trigger failures.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free