react-scan icon indicating copy to clipboard operation
react-scan copied to clipboard

perf: optimize overlay rendering and state handling

Open pivanov opened this issue 1 year ago • 5 comments

This optimization focuses on improving the element inspection overlay performance and code clarity. Key changes:

  1. Introduces element caching to prevent redundant redraws
  2. Consolidates overlay update logic with a unified type parameter
  3. Removes redundant state variables (currentHoveredElement)
  4. Improves event handling order and code organization
  5. Maintains proper state transitions while reducing unnecessary operations

The changes make the inspection tool more efficient and the code more maintainable.

https://github.com/user-attachments/assets/de394826-f44f-4c72-b5eb-0aa490019de9

pivanov avatar Dec 02 '24 20:12 pivanov

@pivanov is attempting to deploy a commit to the Million Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Dec 02 '24 20:12 vercel[bot]

wow this is fast asf

aidenybai avatar Dec 03 '24 00:12 aidenybai

wow this is fast asf

Yep :) I have few more ideas but I will wait to merge (or improve this one if needed) and I will bring the rest ;)

pivanov avatar Dec 03 '24 00:12 pivanov

I changed from tracking window-level metrics (scroll position, window size) to directly comparing element positions using getBoundingClientRect(). Here's why I think this is a better approach:

More accurate position tracking

  • getBoundingClientRect() gives me the element's position relative to the viewport
  • Catches all types of position changes (scrolling, transforms, resizing)
  • Works correctly with scrollable containers at any level of nesting

More reliable

  • Window metrics could miss position changes from container scrolling
  • Previous approach wouldn't catch CSS transforms
  • Element's actual position is more important than what caused the change

Simpler code

  • I removed multiple tracking variables
  • I removed separate scroll event listeners
  • I now have a single source of truth for element position

Better edge case handling

  • Works with nested scrollable containers
  • Handles CSS transforms properly
  • More accurate for complex layouts

Tradeoff: Slightly more frequent getBoundingClientRect() calls, but since I'm already in a RAF loop and only checking one element, the performance impact is minimal compared to the benefits of more reliable position tracking.


https://github.com/user-attachments/assets/2bdd0285-3183-4f3c-bdb8-994b3f6f1043

pivanov avatar Dec 03 '24 05:12 pivanov

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-scan ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 4, 2024 11:17pm

vercel[bot] avatar Dec 04 '24 23:12 vercel[bot]

This one is obsolate now :)

pivanov avatar Dec 26 '24 07:12 pivanov