Repository navigation
Description
Summary
The computer's search spends 60 to 85% of its time generating and legality-checking moves that it then throws away. The Hard level and the Hint button reach only depth 3 in typical middlegames within their one-second budget.
Where
js/ai.js:161(quiescencegenerates every legal move and keeps only captures and queen promotions)js/ai.js:116(searchMoves)js/rules.js:203(legalMovesFromtests all 64 target squares per piece)js/rules.js:250(legality check copies the board for every candidate)js/rules.js:46(isInCheckscans 64 squares)
Details
const forcing = searchMoves(position, color).filter(
(move) => position.board[move.to.row][move.to.col] !== null || move.promotion === QUEEN
);Measured with Node on a desktop, fixed depth-4 search of a middlegame position (r1bq1rk1/pp2bppp/2n1pn2/2pp4/3P1B2/2PBPN2/PP1N1PPP/R2Q1RK1 w): 2004 ms in total, of which 1379 ms was move generation inside quiescence (34,144 calls, 1,237,367 legal moves generated, 87,176 kept, 93% discarded) and 301 ms was generation in the main search. Overall speed is about 60 to 70 thousand nodes per second. There is no transposition table, so each deepening pass searches every position again.
Impact
Hard (1000 ms budget) and Hint (HINT_LEVEL: 'hard') finish depth 3 in 77 to 198 ms and do not finish depth 4 within the budget in three of five middlegame positions tested. The level's base depth is not cut short by the clock and took up to 438 ms in a tactical position on a desktop; on a phone it will take longer (not measured). Where a Worker is not available, the same search blocks the page for that long. The cost is playing strength per second and reply time on slow devices.
Suggested fix
In order of payoff:
- Give quiescence a captures-only generator: walk each piece's rays or offsets and emit only moves onto enemy pieces plus queen promotions.
- Generate pseudo-legal moves in the search and test legality only when a move is actually searched (after
applyMove, skip it if the mover's king is attacked). - Replace the 64-square target scan in
legalMovesFromwith per-piece ray or offset walks, and make the attack test look outward from the target square. - Optionally add a small transposition table keyed by
positionKey.
Steps 1 and 2 are local to ai.js plus one new function in rules.js and leave the UI's use of legalMovesFrom untouched.
Acceptance criteria
- The depth-4 search of the position above takes at most half the time it takes now on the same machine.
- The perft counts and AI tests in
tests/unit/rules.test.js,tests/unit/ai.test.jsandtests/unit/ai-strength.test.jsstill pass. - The search returns the same best move at fixed depth for the existing test positions.
Getting started
- Relevant files:
js/ai.js,js/rules.js,tests/unit/ai-strength.test.js - How to run tests:
npm ci, thennpm test - Related docs:
docs/ARCHITECTURE.md(the engine section)
Filed by repo-audit/full-audit at ac3afa3. Severity: medium. Effort: M. audit-key: performance|js/ai.js|quiescence
Activity
TMHSDigital commentedon Nov 15, 2026
Done in the commit that closed this issue ("Stop the search generating moves it throws away").
What changed:
allLegalCapturesinrules.jsfinds only captures and pawn arrivals on the last rank, and quiescence uses it.isSquareAttackedlooks outward from the target square (pawn, knight, king, then the first piece along each line).Measured with Node on one desktop, fixed depth-4 search, three runs per position, time per search:
r1bq1rk1/pp2bppp/2n1pn2/2pp4/3P1B2/2PBPN2/PP1N1PPP/R2Q1RK1 w(from this issue)r2q1rk1/ppp2ppp/2npbn2/2b1p3/2B1P3/2NP1N2/PPP2PPP/R1BQ1RK1 wr1bqkb1r/pppp1ppp/2n2n2/4p2Q/2B1P3/8/PPPP1PPP/RNB1K1NR w2r3k1/pp3ppp/2n1b3/3p4/3P4/2P1BN2/P4PPP/R5K1 bThe chosen move was the same before and after in every position, for three fixed random sources.
Verified: perft counts,
ai.test.jsandai-strength.test.jspass unchanged. New tests intests/unit/rules.test.jscompareallLegalCaptureswith the filtered full generator (same moves, same order) andisSquareAttackedwith a piece-by-piece reference on every position within two moves of the four perft positions. Full suite (287 tests), lint and format check pass.Not done: pseudo-legal generation in the main search (step 2), ray walks in
legalMovesFrom(rest of step 3) and a transposition table (step 4). Timing on a phone was not measured.Stop the search generating moves it throws away (TMHSDigital#86)