やってみた

新しいJavaScriptランタイムのBunを試してみました

こんにちは中川です。今回は、最近登場したJavaScriptランタイムのBunを試してみました。

■Bunとは

https://bun.sh/

Bun は、JavaScriptとTypeScriptのプロジェクトのオールインワンな新しいJavaScriptランタイムで、ネイティブのバンドル、トランスパイル、タスクランナー、npm クライアントを内蔵しています。

また、JSエンジンは、JavaScriptCoreが使用されていて、NodeJS や Deno のV8とは違ったJSエンジンとなっています。速度面にも気を使って開発されているようで、他のランタイムと比べて速いとアピールされています。

■インストール

手元のMacBook環境で試してみました。

以下のコマンドで数秒でインストールは完了しました。

$ curl -fsSL https://bun.sh/install | bash
######################################################################## 100.0%
bun was installed successfully to ~/.bun/bin/bun

Manually add the directory to ~/.bashrc (or similar):
  export BUN_INSTALL="$HOME/.bun"
  export PATH="$BUN_INSTALL/bin:$PATH"

To get started, run:

  bun --help

※この環境ではbashを使っているので上記の環境変数設定を .bashrcに 追記しました。

$ bun -v
0.1.10

インストール完了です。

■動作確認

それでは、公式の最初の例にもあるように、HTTPサーバーで動作確認をしてみます。

// http.ts
export default {
    port: 3000,
    fetch(request: Request) {
        return new Response("Hello World")
    }
}
$ bun ./http.ts
$ curl -v 127.0.0.1:3000
*   Trying 127.0.0.1:3000...
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:3000
> User-Agent: curl/7.79.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 HM
< content-type: text/plain;charset=utf-8
< Content-Length: 11
< 
* Connection #0 to host 127.0.0.1 left intact
Hello World

さくっとHTTPサーバーの動作確認が行えました。

■速度比較

ついでに、試しにNodeJSでも同じようにhttpサーバを立てて、abを使って簡単な速度比較をしてみました。

// node_http.js
const http = require('http')

const server = http.createServer((req, res) => {
    res.end("Hello World")
})

server.listen(3001)

●Bun

$ ab -n 5000 -c 10 http://127.0.0.1:3000/
#...省略
Server Software:
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      10
Time taken for tests:   0.237 seconds
Complete requests:      5000
Failed requests:        0
Total transferred:      450000 bytes
HTML transferred:       55000 bytes
Requests per second:    21091.08 [#/sec] (mean)
Time per request:       0.474 [ms] (mean)
Time per request:       0.047 [ms] (mean, across all concurrent requests)
Transfer rate:          1853.71 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     0    0   0.1      0       1
Waiting:        0    0   0.1      0       1
Total:          0    0   0.1      0       1

●NodeJS

$ ab -n 5000 -c 10 http://127.0.0.1:3001/
#...省略
Server Software:
Server Hostname:        127.0.0.1
Server Port:            3001

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      10
Time taken for tests:   0.367 seconds
Complete requests:      5000
Failed requests:        0
Total transferred:      430000 bytes
HTML transferred:       55000 bytes
Requests per second:    13611.92 [#/sec] (mean)
Time per request:       0.735 [ms] (mean)
Time per request:       0.073 [ms] (mean, across all concurrent requests)
Transfer rate:          1143.19 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     0    1   0.2      1       3
Waiting:        0    0   0.2      0       2
Total:          0    1   0.2      1       3

上記の通り、Bunのほうが、1.5倍ほど高速な結果となりました。
※手元でのごくごく簡単なプログラムでの計測となりますので、実際の運用レベルの用途では様々な要因で単純に速くなるとは限りませんのでご注意ください。

■さいごに

新しいJavaScript(TypeScript)の実行環境のBunを試してみました。

まだ正式リリースはされておらず、実験的なソフトウェアという状況ですので、本番環境での利用は難しいかもしれませんが今後が楽しみとなっています。

author img for nakagawayoshiki

nakagawayoshiki

前の記事へ

次の記事へ

一覧へ戻る
PAGE TOP