Skip to content

AI search spends most of its time generating moves it discards #86

Closed

Description

@TMHSDigital

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 (quiescence generates every legal move and keeps only captures and queen promotions)
  • js/ai.js:116 (searchMoves)
  • js/rules.js:203 (legalMovesFrom tests all 64 target squares per piece)
  • js/rules.js:250 (legality check copies the board for every candidate)
  • js/rules.js:46 (isInCheck scans 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:

  1. Give quiescence a captures-only generator: walk each piece's rays or offsets and emit only moves onto enemy pieces plus queen promotions.
  2. 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).
  3. Replace the 64-square target scan in legalMovesFrom with per-piece ray or offset walks, and make the attack test look outward from the target square.
  4. 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.js and tests/unit/ai-strength.test.js still 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, then npm 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

  1. added
    help wanted
    performance
    severity: medium
    on Nov 15, 2026
  2. TMHSDigital commented on Nov 15, 2026

    @TMHSDigital
    OwnerAuthor

    Done in the commit that closed this issue ("Stop the search generating moves it throws away").

    What changed:

    • Step 1 of the suggested fix: allLegalCaptures in rules.js finds only captures and pawn arrivals on the last rank, and quiescence uses it.
    • Part of step 3: isSquareAttacked looks 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:

    Position Before After
    r1bq1rk1/pp2bppp/2n1pn2/2pp4/3P1B2/2PBPN2/PP1N1PPP/R2Q1RK1 w (from this issue) 2604 ms 670 ms
    r2q1rk1/ppp2ppp/2npbn2/2b1p3/2B1P3/2NP1N2/PPP2PPP/R1BQ1RK1 w 605 ms 148 ms
    r1bqkb1r/pppp1ppp/2n2n2/4p2Q/2B1P3/8/PPPP1PPP/RNB1K1NR w 172 ms 54 ms
    2r3k1/pp3ppp/2n1b3/3p4/3P4/2P1BN2/P4PPP/R5K1 b 1134 ms 328 ms

    The chosen move was the same before and after in every position, for three fixed random sources.

    Verified: perft counts, ai.test.js and ai-strength.test.js pass unchanged. New tests in tests/unit/rules.test.js compare allLegalCaptures with the filtered full generator (same moves, same order) and isSquareAttacked with 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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedperformanceseverity: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions