Game engines in a browser tab · Part 2 of 3
Grading chess moves for humans
Putting a chess engine in a Web Worker is the easy half of a browser chess app. The hard half starts once the engine talks. Stockfish reports positions in centipawns, hundredths of a pawn, which is the right unit for searching a tree and the wrong one for judging a person. And at full strength it plays at around 3,500 Elo, so a beginner opponent has to be weakened without turning into something that randomly gives away its queen.
web-chess runs Stockfish 18 in the tab for playing, importing, and reviewing games. The figures below run the app’s own sizing and grading code, on evaluations recorded from the same Stockfish build in Node.
Three engines in one tab
The app talks to each engine in UCI, the plain-text protocol chess engines speak. It keeps separate engines in separate workers instead of sharing one, because the jobs want different settings. Live analysis wants several candidate lines (MultiPV) and win/draw/loss estimates. The opponent wants UCI_LimitStrength and an Elo cap. And switching one engine between jobs isn’t free: re-sending an option like Hash, even with the same value, clears Stockfish’s transposition table, its memory of positions it has already searched.
A full game review gets a temporary pool of two to four engines. Each one takes a contiguous stretch of the game, so its table stays warm from one move to the next. On the Opera Game at depth 18, that halves the review, from 16.5 seconds on one engine to 8.2 on four.
Sized to the device
How many engines and threads the app starts depends on what the browser reports. A phone gets one small single-threaded engine, a 16 MB hash table, and a single line of analysis, since each extra line costs two to four times the search. An 18-core desktop gets four review engines with two threads each.
Threads need shared memory, and browsers only allow it on cross-origin isolated pages, which GitHub Pages can’t make on its own; the app’s service worker adds the headers. Without isolation, every engine drops to one thread, but the review pool still runs at full width, because separate workers never shared memory anyway.
That single-thread fallback was supposed to be rare. For a long time it was the only path anyone got: a missing bootstrap wrapper made the multithreaded build die at startup, and the app fell back without a word. Nothing looked broken. Fixing it took a depth-16 search from 443,000 to 3.5 million nodes a second.
From a score to a verdict
Losing a pawn in a level endgame can cost you the game. Losing a pawn when you’re already a rook up changes nothing. Centipawns can’t tell those apart. Winning chances can.
web-chess converts each evaluation into a winning chance with Lichess’s logistic curve, grades every move twice (on centipawns lost and on winning chances lost), and reports the milder verdict. Three pawns from a level position cuts your chances from 50% to 25%: a Blunder on both ladders. The same three pawns at +10 takes you from 97% to 93% and is graded Good.
Even walking into mate when already lost (15…Nxd7 in the Opera Game) comes out Good. Stockfish encodes a forced mate as a 100-pawn score, so on the raw scale this move looks like a 90-pawn catastrophe. That sentinel once pushed the Opera Game’s average centipawn loss to 316 in a report that also counted zero mistakes. The winning-chance curve sees what actually happened: a lost position ended one move sooner.
One number for a game
Turning forty per-move scores into one accuracy number looks like a job for an average, and for its first seven months web-chess used one. The trouble is that one blunder in forty moves barely dents a mean. Thirty-nine solid moves at 95 and one game-losing blunder at 9.5 still average 93, which reads like a near-perfect game.
Following Lichess, web-chess now averages two other summaries. The first weights each move by how volatile the game was around it (the standard deviation of winning chances over a local window), so moves made while the balance is swinging count far more than routine moves in a dead ending. The second is the harmonic mean, which the worst move dominates. Together they pull the one-blunder game from 93 down to 85. Drag the blunder bar back up and the penalty disappears as soon as the mistake stops being fatal.
Reviewing a game
Put the worker pool, the two grading ladders, and the opening book together and you get the review above. The default game was played between two of the app’s own levels, Club as White and Intermediate as Black. Black was winning for most of it until White played 19.Nxf6+. Instead of recapturing, Black stepped away with 19…Kf8, giving up 8.6 pawns and 66 points of winning chances in one move: the only Blunder in either game.
Switch to the Opera Game and Black’s worst moves (6…Nf6 and 10…cxb5) are only Inaccuracies. After 10…cxb5 the engine already has Black four pawns down, so the famous fireworks that follow cost Black very few winning chances it still had.
Not every speedup on this screen survived measurement. Stepping through a reviewed game redraws its evaluation graph on every key press, and splitting the graph’s static half into its own memoized component read 68 ms a press down to 43 ms on one run: a 37% win. Run five times on each build, the version without the split had the lower median:
The spread inside one build was larger than the gap between builds, so the 37% was noise, and the change was reverted. A single timing on a machine this noisy isn’t a result yet.
Playing badly on purpose
Stockfish’s built-in UCI_LimitStrength only goes down to 1,320 Elo, which still flattens a casual player. To soften the two weakest levels, Beginner and Novice, web-chess originally played a uniformly random legal move 38% and 20% of the time.
No beginner chooses moves by rolling a die over every legal move on the board. After 4…Bxf3 in the Opera Game, White has to take back the bishop, and 33 of White’s 35 legal moves leave it hanging and lose three pawns or more. Under the old rule, Beginner walked into one of those more than a third of the time.
Now the opponent asks Stockfish for its top four lines and only varies among moves within 1.8 pawns of its first choice (0.9 for Novice). Here that means choosing between the two sensible recaptures, Qxf3 and gxf3. Across all 90 positions in both games, Beginner went from throwing away three or more pawns on one move in six to never doing so.
Every section of this post is the same fix in a different place. Stockfish’s raw number is right for Stockfish and wrong for the person reading it, so the app translates: winning chances instead of centipawns, a volatility-weighted accuracy instead of a plain mean, and a weakened opponent that still picks between sensible moves.
Piece artwork: Colin M.L. Burnett's set, as drawn by react-chessboard in the app.