← WeRide Interview Insights

WeRide·Frontend Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
Jun 2026

Summary

Weride frontend interview had me implement a custom React hook from scratch, which sounds manageable until you're live on a call and someone's watching you think through rAF cleanup and tab visibility edge cases simultaneously.

Questions Asked (1)

Q1

Build a custom React hook called useFps() that tracks and returns the current frames-per-second of the page using requestAnimationFrame, updates on a rolling time window, and cleans up properly on unmount. Also discuss how it behaves when the tab is backgrounded and how you'd prevent unnecessary re-renders.

System DesignTechnical Trade-offsAPI & Integrations
Author's notes

I got the basic loop working pretty fast, timestamps in a ref, count frames over a second, setState with the result.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the hook's API and requirements, then walk through the implementation using requestAnimationFrame and a rolling time window. Explain how you handle tab visibility changes and prevent unnecessary re-renders, and finally discuss trade-offs and edge cases.

Pro tip: Use a ref to store the frame count and a state that updates only when the FPS value changes, and throttle updates to at most once per second to avoid excessive re-renders. Also, listen to the Page Visibility API to pause the loop when the tab is hidden, preventing misleading FPS drops.

1. Clarify requirements and API

Confirm what the hook should return (e.g., a number) and how often it should update. Discuss whether it should start/stop automatically or be controlled by the component.

2. Implement the core loop

Use requestAnimationFrame to increment a frame counter and track elapsed time. Calculate FPS over a rolling window (e.g., last second) and update state only when the value changes.

3. Handle cleanup and visibility

Cancel the animation frame on unmount. Use the Page Visibility API to pause the loop when the tab is hidden and resume when visible, resetting the window to avoid skewed results.

4. Optimize re-renders

Store the frame count in a ref and only update state when the FPS value changes. Throttle state updates to at most once per second to minimize re-renders.

5. Discuss trade-offs and edge cases

Mention potential issues like high-frequency updates, accuracy of the rolling window, and how the hook behaves with multiple instances. Suggest possible improvements like using a Web Worker for heavy calculations.

Key Points to Mention

  • Use requestAnimationFrame for accurate frame timing and automatic pausing when tab is backgrounded.
  • Implement a rolling time window (e.g., 1000ms) to calculate FPS, resetting the frame count and timestamp accordingly.
  • Clean up by canceling the animation frame in the useEffect cleanup function to prevent memory leaks.
  • Leverage the Page Visibility API to detect when the tab is hidden and pause the FPS calculation, avoiding misleading low FPS values.
  • Prevent unnecessary re-renders by storing the frame count in a ref and only updating state when the FPS value changes, possibly throttling updates.
  • Consider edge cases: multiple hook instances, server-side rendering, and performance impact of frequent state updates.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.