Skip to content

perf: keep handlers stable and give each square its own style - #243

Open
bdumitrua wants to merge 2 commits into
Clariity:mainfrom
bdumitrua:perf/stable-callbacks-styles
Open

bdumitrua wants to merge 2 commits into
Clariity:mainfrom
bdumitrua:perf/stable-callbacks-styles

Conversation

@bdumitrua

@bdumitrua bdumitrua commented Dec 2, 2026 •

Copy link
Copy Markdown

Description

This branch contains the commit from #242 plus one more (perf: keep handlers stable and give each square its own style). There are two ways to merge:

After #242 , two common cases still re-render the whole board:

  • inline callbacks, or a new options object on every render of the parent;
  • any change to squareStyles, for example a highlight moving to another square.

This PR fixes both. The public API does not change.

How it works

All in ChessboardProvider.tsx unless noted.

  1. Stable callbacks.
    • onSquareClick, onPieceClick, onMouseOverSquare and the other handlers that squares call are wrapped with useLatestHandler: a function that keeps the same identity and calls the latest handler from a ref. An inline onSquareClick: () => ... therefore no longer changes the settings context on each render.
    • Whether a handler is set still updates, because squares check onX?.().
    • Handlers that only the provider calls (onPieceDrop, onPieceDrag, onPieceDragCancel, onArrowsChange) are read from optionsRef at call time.
  2. canDragPiece through a small store.
    • It is called during render (it decides whether a piece is draggable and which cursor it shows), so reading it from a ref would leave pieces stale when it changes.
    • The predicate goes into a store, and each Draggable and Piece subscribes with useSyncExternalStore (useCanDragPiece). A piece re-renders only if its own true/false result changes.
    • A new inline predicate with the same answers re-renders nothing. A change of turn re-renders only the pieces whose permission flipped.
    • useChessboardContext() still returns canDragPiece from its own small context.
  3. squareStyles per square (Board.tsx, Square.tsx).
    • squareStyles moves out of the settings context into its own context, which only Board reads.
    • Board passes squareStyles[squareId] to each square as a prop, so memo re-renders only the squares whose style object changed.

Type of change

  • Performance improvement

How Has This Been Tested?

Median work per update on an 8×8 board, #242 → this PR, in ms:

Scenario MacBook MacBook, 6× CPU throttling Android phone
100 parent re-renders with inline options and callbacks 1.9 → 0.9 4.5 → 2.3 5.1 → 2.3
Move a highlight across 50 squares 2.0 → 1.2 4.6 → 2.8 5.2 → 2.8

The gain grows with the number of squares. On a 16×16 board, stock 5.12.4 → both PRs:

Scenario MacBook MacBook, 6× CPU throttling Android phone
Replay 60 moves 5.0 → 2.3 33.0 → 7.5 18.9 → 3.8
100 parent re-renders with the same options 3.8 → 1.0 18.1 → 3.0 9.7 → 2.4
100 parent re-renders with inline options and callbacks 3.9 → 1.1 15.85 → 3.05 9.3 → 2.6
Move a highlight across 50 squares 4.0 → 1.5 17.15 → 4.2 9.25 → 3.2

With 6× throttling on 16×16, stock no longer fits a 60 fps frame during the replay (median frame 38.4 ms); with both PRs it does (17.7 ms). Dragging is dominated by dnd-kit and stays about the same.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@vercel

vercel Bot commented Dec 2, 2026

Copy link
Copy Markdown

@bdumitrua is attempting to deploy a commit to the Ryan Gregory's projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant