oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.13k stars 2.76k forks source link

Bun is Slower than Node with Recursive Negamax Function. #1428

Open eli-rich opened 2 years ago

eli-rich commented 2 years ago

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:

  1. Clone this repo.
  2. bun install
  3. time node index.js
  4. time bun index.js
  5. Compare results.

The relevant code comes from my npm package.

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):

  1. 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
  2. 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):

  1. 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
    1. 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

Additional information

I created a github repo to easily replicate this.

https://github.com/BlazingFire007/bun-benchmark-negamax

eli-rich commented 2 years ago

To be clear I had the same results on my old gaming PC running elementary OS 6.1.

The results of that test are included.