Skip to content

Commit ad78ede

Browse files
authored
perf(ai): Run AI iterations using setImmediate (#999)
Use `setImmediate` to run each async iteration of the MCTS bot. In most scenarios this will use a polyfill as `setImmediate` is not widely supported or on a web standards track. See <https://github.com/YuzuJS/setImmediate> for browser support details.
1 parent 7651628 commit ad78ede

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
"react-cookies": "^0.1.0",
162162
"redux": "^4.1.0",
163163
"rfc6902": "^4.0.2",
164+
"setimmediate": "^1.0.5",
164165
"socket.io": "^4.1.3",
165166
"socket.io-client": "^4.1.3",
166167
"svelte": "^3.41.0",

src/ai/mcts-bot.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* license that can be found in the LICENSE file or at
66
* https://opensource.org/licenses/MIT.
77
*/
8+
import 'setimmediate';
89
import { CreateGameReducer } from '../core/reducer';
910
import { Bot } from './bot';
1011
import type { BotAction } from './bot';
@@ -313,7 +314,7 @@ export class MCTSBot extends Bot {
313314
const asyncIteration = () => {
314315
if (this.iterationCounter < numIterations) {
315316
iteration();
316-
setTimeout(asyncIteration, 0);
317+
setImmediate(asyncIteration);
317318
} else {
318319
resolve(getResult());
319320
}

0 commit comments

Comments
 (0)