]> git.sesse.net Git - remoteglot/commitdiff
Faster and smaller update_imbalance().
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 27 Dec 2022 17:15:54 +0000 (18:15 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 27 Dec 2022 17:15:54 +0000 (18:15 +0100)
www/js/remoteglot.js

index 27ded16052cb45076f37b00534f1f448f20bfdfc..be32c44fb27cb8f370773016373d1ae4efe6c7d7 100644 (file)
@@ -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 = '';