I am trying to write a simple chess engine in Javascript. It performs very slowly but I have finally rooted down the problem to check detection. How might I speed up this check detection? If you have any questions or need any clarifications feel free to ask.

Check detection takes approximately 10-20 milliseconds.

Here is the code:

function availableInvalidSpots(board, player) {
    let validPositions = [];
    let playerPositions = getAllPieces(player, board);
    for (position = 0; position < playerPositions.length; position++) {
        validPositions.push(getUncheckedMoves(playerPositions[position], board));
    return validPositions;

function getPiecePosition(piece, board) {
    for (rowCount = 0; rowCount < 8; rowCount++) {
        for (columnCount = 0; columnCount < 8; columnCount++) {
            if (board[rowCount][columnCount].pieceName == piece) {
                return {
                    x: columnCount,
                    y: rowCount

function getPlayerInfo(pieceType) {
    return (pieceType == blackPieces ? {
        king: "blackKing",
        opponent: whitePieces
    } : {
        king: "whiteKing",
        opponent: blackPieces

function setPlayerInfo(player, board) {
    let PLAYER = getPlayerInfo(player);
    let opponent = PLAYER.opponent;
    king = PLAYER.king;
    kingPosition = getPiecePosition(king, board);
    opponentMoves = availableInvalidSpots(board, opponent);

function isPlayerInCheck(player, board) {
    setPlayerInfo(player, board);

    for (opponentMove = 0; opponentMove < opponentMoves.length; opponentMove++) {
        for (square = 0; square < opponentMoves[opponentMove].move.length; square++) {
            if (opponentMoves[opponentMove].move[square].to.x == kingPosition.x && opponentMoves[opponentMove].move[square].to.y == kingPosition.y) {
                return true;
    return false;

It looks like your engine is using very simple for loop and arrays. Your code simply loop over again and again, possibly repeat each search iteration.

That’s obviously not good. Chess engine can’t run fast if it has to loop like that. If the position had no check surely all CPU time wasted?

My recommendation is change your data structure to a bitboard. Bitboard will allow you run check detection quickly, just do a AND the pieces your opponent can attack and your king position.

  • Doesn't JavaScript only give float number types? How would you make a bitboard using them? Jan 28 '20 at 23:12
  • @OscarSmith I'm not JS expert. I coded my engine in C++, never did it on JS. You may be right, a JS expert is needed here. 64-bit integer representation in JS sounds more like a Stackoverflow problem. In any case I think JS is a bad choice for chess engine. One could just do it in C++ and compile it to JS.
    – SmallChess
    Jan 28 '20 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.