From: Steinar H. Gunderson Date: Tue, 27 Dec 2022 17:15:54 +0000 (+0100) Subject: Faster and smaller update_imbalance(). X-Git-Url: https://git.sesse.net/?p=remoteglot;a=commitdiff_plain;h=a672cdf742583ecb42116f3b5e12c18dd3b37cf5 Faster and smaller update_imbalance(). --- diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 27ded16..be32c44 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -1827,23 +1827,19 @@ let update_historic_analysis = function() { * @param {string} fen */ let update_imbalance = function(fen) { - let hiddenboard = new Chess(fen); let imbalance = {'k': 0, 'q': 0, 'r': 0, 'b': 0, 'n': 0, 'p': 0}; - for (let row = 0; row < 8; ++row) { - for (let col = 0; col < 8; ++col) { - let col_text = String.fromCharCode('a1'.charCodeAt(0) + col); - let row_text = String.fromCharCode('a1'.charCodeAt(1) + row); - let square = col_text + row_text; - let contents = hiddenboard.get(square); - if (contents !== null) { - if (contents.color === 'w') { - ++imbalance[contents.type]; - } else { - --imbalance[contents.type]; - } - } + for (const c of fen) { + if (c === ' ') { + // End of board + break; + } + if (c != c.toUpperCase()) { + --imbalance[c]; + } else if (c != c.toLowerCase()) { + ++imbalance[c.toLowerCase()]; } } + let white_imbalance = document.getElementById('whiteimbalance'); let black_imbalance = document.getElementById('blackimbalance'); white_imbalance.textContent = '';