Skip to content

perf: split board context and memoize squares - #242

Open
bdumitrua wants to merge 1 commit into
Clariity:mainfrom
bdumitrua:perf/split-context-memo
Open

bdumitrua wants to merge 1 commit into
Clariity:mainfrom
bdumitrua:perf/split-context-memo

Conversation

@bdumitrua

@bdumitrua bdumitrua commented Dec 2, 2026 •

Copy link
Copy Markdown

Description

Today every update re-renders the whole board: a new position, an unrelated re-render of the parent, or a drag. This PR makes the board re-render only the squares that changed.

This is part 1 of 2. You can merge them one after the other, or skip this one and merge only #243, which contains this commit plus the second change.

The public API does not change: the same exports, and useChessboardContext() returns the same object as before.

How it works

  1. One context becomes three (ChessboardProvider.tsx).

    • ChessboardSettingsContext holds options and callbacks and changes rarely.
    • ChessboardLiveContext holds the position, the dragged piece and arrows in progress, and changes on every move.
    • ChessboardDragContext holds allowDragging and canDragPiece.

    Each component reads only what it needs, so a move no longer re-renders every piece and square through the context. useChessboardContext() merges the three, so code outside the library sees the same object as before.

  2. Squares get plain props and are memoized (Board.tsx, Droppable.tsx, Square.tsx).

    • Droppable used to take a render-prop child, which is a new function on every render, so memo on Square never helped.
    • Board now computes the few values each square needs and passes them as props: the piece type, whether the piece is the drag ghost, and where it animates to. Droppable and Square are memoized, so React skips the squares whose props did not change.
    • Piece gets ghost and moveTo as props instead of reading draggingPiece and positionDifferences from context.
  3. Event handlers read live state at event time.

    • Click, mouse and touch handlers in Square, and the drag handlers in the provider, read the current position from a ref (liveRef) when the event fires, instead of subscribing to it during render.
    • Only the square under the dragged piece subscribes to live state, so that onMouseOverSquare still fires during a drag.
  4. Stable defaults. The default squareStyles = {}, arrows = [], boardStyle and the sensor options were new objects on every render, which changed the context each time. They are memoized now.

  5. Tap detection on touch screens (Square.tsx).

    • The isClickingOnMobile state needed a re-render on every touch and an effect that watched draggingPiece.
    • It is replaced by a counter of drag starts: touchstart records the counter, and touchend fires the click only if no drag started in between. The behavior is the same.
  6. squareRenderer receives the piece object only when a renderer is set, so squares without one do not depend on it.

About the size: a large part of the ChessboardProvider.tsx diff is the dependency list of the memoized settings object and re-indentation from the nested providers.

docs/stories/performance/RenderCounts.stories.tsx (273 lines) is a Storybook story that counts renders per scenario. It is docs only; I can drop it if you'd rather not keep it.

Type of change

  • Performance improvement

How Has This Been Tested?

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

Scenario MacBook MacBook, 6× CPU throttling Android phone
Replay 60 moves 2.95 → 1.10 11.0 → 6.2 8.0 → 3.65
100 parent re-renders with the same options 2.0 → 0.9 5.0 → 2.3 5.7 → 2.2

These numbers come from a bare page with just the board. In a real app the main thread is also busy with the rest of the UI, state updates and often a running chess engine (sometimes more than one), so I'd expect the absolute savings there to be larger. The gain also grows with board size; #243 has 16×16 numbers for both changes together.

This PR alone does not help with inline callbacks or changing squareStyles; #243 covers those. Dragging is dominated by dnd-kit's own work 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