最近看到了一個新玩意「Bun」,覺得很有趣,就玩了一下看看,如果是從node.js的程式搬過來,簡直可以不改任何設定、任何程式就可以輕鬆運行了,也可以繼續用npm、yarn這些套件,用一用還一度覺得,這跟node.js有什麼差別嗎?所以實測了一下效能,才發現,「挖!有差別,厲害,有意思!」
我就用之前測別的東西的指令,一樣的參數,一樣的機器測了一下,可看這篇文章,指令如下
ab -n 1000 -c 1000 -t 10 http://example.com/
那這次我測的程式就一般網頁,只有單純吐出靜態html、js、css檔,下面是測試結果
用node.js執行的結果
Concurrency Level: 1000
Time taken for tests: 10.021 seconds
Complete requests: 12240
Failed requests: 0
Total transferred: 34381125 bytes
HTML transferred: 30673725 bytes
Requests per second: 1221.41 [#/sec] (mean)
Time per request: 818.725 [ms] (mean)
Time per request: 0.819 [ms] (mean, across all concurrent requests)
Transfer rate: 3350.43 [Kbytes/sec] received
接著是用bun執行的結果
Concurrency Level: 1000
Time taken for tests: 10.000 seconds
Complete requests: 20353
Failed requests: 0
Total transferred: 55482278 bytes
HTML transferred: 49844497 bytes
Requests per second: 2035.28 [#/sec] (mean)
Time per request: 491.334 [ms] (mean)
Time per request: 0.491 [ms] (mean, across all concurrent requests)
Transfer rate: 5418.13 [Kbytes/sec] received
結果看起來速度明顯快了兩倍,真的是有點料喔!
然後又測了一下打api,後端讀database的狀況,這是node.js執行的結果
Concurrency Level: 1000
Time taken for tests: 10.098 seconds
Complete requests: 7044
Failed requests: 0
Total transferred: 31026198 bytes
HTML transferred: 24330264 bytes
Requests per second: 697.58 [#/sec] (mean)
Time per request: 1433.522 [ms] (mean)
Time per request: 1.434 [ms] (mean, across all concurrent requests)
Transfer rate: 3000.58 [Kbytes/sec] received
這是bun執行的結果
Concurrency Level: 1000
Time taken for tests: 10.000 seconds
Complete requests: 13640
Failed requests: 0
Total transferred: 56824240 bytes
HTML transferred: 45448480 bytes
Requests per second: 1363.95 [#/sec] (mean)
Time per request: 733.164 [ms] (mean)
Time per request: 0.733 [ms] (mean, across all concurrent requests)
Transfer rate: 5549.04 [Kbytes/sec] received
也是一樣差不多快了兩倍,雖然測效能這種事,也很難完全說在實際運作上就能印證出來,不過單就這個測試Case,我是覺得蠻值得試著用在線上正式環境的,但前提當然是線上正式環境是運行node.js,才能方便轉換使用。
我自己有個網站如今就是使用node.js運行,平常線上人數有100多人,剛用了一下,看得出來的差別只有使用的記憶體,因為pm2會列出來,兩者也大概bun會多用了100mb的記憶體左右,但以這麼無痛轉移就能獲得兩倍的效能,我想應該是值得用的,不過這樣加快的效能,其實在前端一般使用者瀏覽起來,就我自己的感覺,是感覺不太出來,可能在高併發,多人同時在線的時候,會比較有差。