0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【祝ts実行ネイティブサポート】nodeコマンドによるts実行速度をbun、denoと簡単比較

Posted at

結論

node Results:
  Average: 1033ms
bun Results:
  Average: 1033ms
deno Results:
  Average: 1048ms

他ランタイムと同じくらいの実行速度?
やりかたがイケてないのか、大きな差異はなかったです。

bunやDenoが気になりつつ、移行するのもなぁ、な人には嬉しいアプデだと思いました。

以下に今回の計測方法を記載しておくので、測り方こうじゃね?があればぜひ教えてください!

計測

環境

//ver確認
> node -v
v23.6.0
> bun -v
1.1.43
> deno -v
deno 2.1.4

PCスペック(あまり強くないものを引っ張ってきた)
CPU: Intel(R) Core(TM) i5-8250U CPU
メモリ: 8.00 GB

テスト対象コード

test.ts
// test.ts
import { z } from 'zod';
import dayjs from 'dayjs';
import _ from 'lodash';
import axios from 'axios';

// zodによる型定義
const UserSchema = z.object({
    id: z.number(),
    name: z.string(),
    email: z.string().email(),
    createdAt: z.date()
});

type User = z.infer<typeof UserSchema>;

// モジュールを使用する関数
function createUser(data: Partial<User>): User {
    const user: User = {
        id: data.id ?? _.random(1, 1000),
        name: data.name ?? 'default',
        email: data.email ?? 'test@example.com',
        createdAt: data.createdAt ?? dayjs().toDate()
    };

    return UserSchema.parse(user);
}

// 実行テスト
const user = createUser({});
console.log('モジュール読み込みテスト完了');

計算したりごにょごにょするのは特に意味がないはずなので、モジュールのロードなどを適当に記載。

計測コード

benchmark.ps1
# 設定
$runtimes = @("node", "bun", "deno")
$iterations = 10

# 各ランタイムをテスト
foreach ($runtime in $runtimes) {
    Write-Host "`nTesting $runtime..."
    $times = @()
    
    # 指定回数実行
    for ($i = 1; $i -le $iterations; $i++) {
        Write-Host "  Run $i of $iterations..."
        
        # ランタイムに応じたコマンドを実行し時間を測定
        $start = Get-Date
        switch ($runtime) {
            "node" {
                Start-Process -FilePath "node" -ArgumentList "test.ts" -NoNewWindow -Wait -RedirectStandardOutput "nul"
            }
            "bun" {
                Start-Process -FilePath "bun" -ArgumentList "run test.ts" -NoNewWindow -Wait -RedirectStandardOutput "nul"
            }
            "deno" {
                Start-Process -FilePath "deno" -ArgumentList "run test.ts" -NoNewWindow -Wait -RedirectStandardOutput "nul"
            }
        }
        $end = Get-Date
        
        # ミリ秒単位の実行時間を計算
        $duration = ($end - $start).TotalMilliseconds
        $times += [math]::Round($duration)
    }
    
    # 平均を計算
    $total = $times | Measure-Object -Sum
    $average = [math]::Round($total.Sum / $iterations)
    
    # 結果を表示
    Write-Host "`n$runtime Results:"
    Write-Host "  Average: ${average}ms"
    Write-Host "  Individual runs:"
    foreach ($time in $times) {
        Write-Host "    ${time}ms"
    }
}

ひっぱりだしてきたPCがwindowsだったのでPS1で実装

結果

node Results:
  Average: 1033ms
  Individual runs:
    1040ms
    1023ms
    1034ms
    1027ms
    1028ms
    1040ms
    1031ms
    1038ms
    1040ms
    1031ms

Testing bun...
  Run 1 of 10...
  Run 2 of 10...
  Run 3 of 10...
  Run 4 of 10...
  Run 5 of 10...
  Run 6 of 10...
  Run 7 of 10...
  Run 8 of 10...
  Run 9 of 10...
  Run 10 of 10...

bun Results:
  Average: 1033ms
  Individual runs:
    1029ms
    1033ms
    1036ms
    1043ms
    1029ms
    1028ms
    1041ms
    1033ms
    1030ms
    1030ms

Testing deno...
  Run 1 of 10...
  Run 2 of 10...
  Run 3 of 10...
  Run 4 of 10...
  Run 5 of 10...
  Run 6 of 10...
  Run 7 of 10...
  Run 8 of 10...
  Run 9 of 10...
  Run 10 of 10...

deno Results:
  Average: 1048ms
  Individual runs:
    1034ms
    1033ms
    1036ms
    1026ms
    1040ms
    1030ms
    1031ms
    1041ms
    1099ms
    1106ms

どれも1秒前後で処理終了
結果は冒頭に記載した通りである

ランタイムで差が出るとしたら、コマンド開始後~js実行までかな?と思い、tsソース自体はシンプルなものを用意したが、認識間違っていたら教えてください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?