It uses the negamax algorithm to find the best move in a tic-tac-toe game.
function negamax(board: Board, depth: number, alpha: number, beta: number): number {
if (board.isOver || depth === 0) {
if (board.winner === -1) return 0;
else {
return board.winner === board.turn ? 100 : -100;
}
}
let bestScore = -Infinity;
for (const move of board.moves) {
board.move(move);
const score = -negamax(board, depth - 1, -beta, -alpha);
board.undo(move);
bestScore = Math.max(score, bestScore);
alpha = Math.max(alpha, score);
if (alpha >= beta) break;
}
return bestScore;
}
Here is the full code from the benchmark:
import { Board, search } from 'exesjs';
for (let i = 0; i < 10; i++) {
console.time('game');
const board = new Board({
width: 6,
height: 6,
winLength: 4,
turn: 1,
});
while (!board.isOver) {
board.move(search(board, 5));
board.move(search(board, 5));
}
console.timeEnd('game');
}
How often does it reproduce? Is there a required condition?
It is consistently slower than node.js
What is the expected behavior?
Expected behavior is to have similar performance to node.js
What do you see instead?
Here are the logs from macOS 12.5 and elementaryOS 6.1:
elementary OS 6.1 (old gaming pc):
NODEJS:
game: 2.372s
game: 2.963s
game: 1.845s
game: 3.067s
game: 5.448s
game: 6.176s
game: 1.561s
game: 2.593s
game: 1.514s
game: 2.434s
node index.js 30.08s user 0.05s system 100% cpu 30.029 total
BUN:
[5.28s] game
[7.16s] game
[7.65s] game
[9.20s] game
[16.72s] game
[5.67s] game
[8.17s] game
[9.73s] game
[9.32s] game
[8.84s] game
bun index.js 88.00s user 0.15s system 100% cpu 1:27.76 total
macOS Monterey 12.5 (macbook pro 2020 intel i5):
NODEJS:
game: 3.601s
game: 7.024s
game: 4.469s
game: 6.390s
game: 6.889s
game: 5.371s
game: 2.786s
game: 3.324s
game: 1.901s
game: 4.944s
node index.js 44.52s user 1.02s system 96% cpu 47.337 total
BUN:
[8.10s] game
[12.46s] game
[7.34s] game
[11.05s] game
[6.68s] game
[7.53s] game
[10.47s] game
[8.93s] game
[7.46s] game
[3.20s] game
bun index.js 81.48s user 1.53s system 99% cpu 1:23.26 total
What version of Bun is running?
0.2.2 (canary)
What platform is your computer?
Darwin 21.6.0 Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:25 PDT 2022; root:xnu-8020.140.41~1/RELEASE_X86_64 x86_64 i386
What steps can reproduce the bug?
To replicate:
bun install
time node index.js
time bun index.js
The relevant code comes from my npm package.
It uses the negamax algorithm to find the best move in a tic-tac-toe game.
Here is the full code from the benchmark:
How often does it reproduce? Is there a required condition?
It is consistently slower than node.js
What is the expected behavior?
Expected behavior is to have similar performance to node.js
What do you see instead?
Here are the logs from macOS 12.5 and elementaryOS 6.1:
elementary OS 6.1 (old gaming pc):
NODEJS:
BUN:
macOS Monterey 12.5 (macbook pro 2020 intel i5):
Additional information
I created a github repo to easily replicate this.
https://github.com/BlazingFire007/bun-benchmark-negamax