bun 写之前nodejs的接口性能会有提升么?对比golang 的 gin 框架写的接口,使用bun来运行nodejs的接口速度有多少差距


bun 写之前nodejs的接口性能会有提升么?对比golang 的 gin 框架写的接口,使用bun来运行nodejs的接口速度有多少差距

Bun是一个新的JavaScript运行时,它声称比Node.js快3倍,比Deno快2倍,而且集成了包管理器,测试运行器和打包器等功能。² Bun使用了JavaScriptCore引擎,而Node.js使用了V8引擎,前者在启动时间和内存占用上有优势。⁴ Bun还提供了一些优化的API和工具,例如自带的React服务端渲染,TypeScript支持,Web标准API等。³

Gin是一个用Go语言编写的高性能的HTTP Web框架,它具有类似于Martini的API,但是性能比Martini快40倍。⁸ Gin使用了httprouter库,这是一个基于Radix树的高效路由器。⁶ Gin还提供了一些方便的功能,例如中间件支持,错误管理,JSON验证,路由分组,渲染等。⁸

对比Bun和Gin,我没有找到直接的性能测试的结果,但是我可以参考一些类似的测试,例如Node.js和Gin,以及Bun和Node.js的测试。根据这篇文章,Node.js和Gin在处理RESTful API的请求时,Gin的吞吐量(每秒请求数)是Node.js的2.5倍左右。根据这篇文章,Bun和Node.js在服务端渲染React的应用时,Bun的吞吐量是Node.js的3倍左右。因此,我推测Bun和Gin在处理HTTP请求时,性能差距不会太大,可能在1倍到2倍之间,具体还要看应用的复杂度和场景。

(1) . https://bing.com/search?q=bun+nodejs+performance.
(2) A first look at Bun: is it really 3x faster than Node.js and Deno?. https://dev.to/builderio/a-first-look-at-bun-is-it-really-3x-faster-than-nodejs-and-deno-45od.
(3) Bun — A fast all-in-one JavaScript runtime. https://bun.sh/.
(4) Gin Web Framework. https://gin-gonic.com/.
(5) [Crazy Go Day] Why Using Gin For Golang Backend? - Medium. https://medium.com/@aaaa102234/crazy-go-day-why-using-gin-for-golang-backend-9ca48ec5d855.
(6) Build RESTful API using Go Gin [In-Depth Tutorial]. https://www.golinuxcloud.com/golang-gin/.
(7) A first look at Bun: is it really 3x faster than Node.js and Deno?. https://dev.to/builderio/a-first-look-at-bun-is-it-really-3x-faster-than-nodejs-and-deno-45od.
(8) A Quick Look at Bun 1.0 – The Node.js Alternative - freeCodeCamp.org. https://www.freecodecamp.org/news/a-quick-look-at-bun-js/.
(9) Deeper testing of Bun's performance and compatibility against Node.js. https://techsparx.com/nodejs/bun/speed-test.html.
(10) . https://bing.com/search?q=gin+golang+performance.
(11) GitHub - gin-gonic/gin: Gin is a HTTP web framework written in Go .... https://github.com/gin-gonic/gin.
(12) undefined. https://www.sitepoint.com/bun-javascript-runtime-introduction/.
(13) undefined. https://medium.com/deno-the-complete-reference/hello-world-performance-bun-vs-node-js-2c9d383b94c1.
(14) undefined. https://bun.sh/install.
(15) undefined. https://masteringbackend.com/posts/gin-framework.
(16) undefined. https://mattermost.com/blog/choosing-a-go-framework-gin-vs-echo/.
(17) undefined. https://blog.logrocket.com/building-microservices-go-gin/.


那么在对服务器的性能要求上 bun 跟 golang 哪个更有优势?比如内存占用,cpu消耗,多线程利用等等

Bun和Golang在服务器的性能要求上有各自的优势和劣势,具体如下:

  • 内存占用:Bun使用了JavaScriptCore引擎,而Golang使用了自己的内存管理器。JavaScriptCore引擎在内存占用上有优势,因为它使用了一种叫做“分代式垃圾回收”的技术,可以有效地回收不再使用的内存空间。⁴ Golang的内存管理器则使用了一种叫做“标记清除”的技术,虽然也可以回收不再使用的内存空间,但是会产生一些内存碎片,导致内存占用增加。 因此,Bun在内存占用上比Golang更有优势。
  • CPU消耗:Bun和Golang在CPU消耗上没有明显的差别,但是Golang可能会稍微更高一些。这是因为Golang是一种编译型语言,而Bun是一种解释型语言。编译型语言在运行时不需要解释器,而解释型语言需要解释器来执行代码。 解释器会增加一些额外的开销,但是也会带来一些优化的机会。 JavaScriptCore引擎使用了一种叫做“即时编译”的技术,可以在运行时将热点代码编译成机器码,从而提高执行效率。 因此,Bun和Golang在CPU消耗上没有太大的差距,但是Golang可能会稍微更高一些。
  • 多线程利用:Golang在多线程利用上比Bun更有优势,因为Golang支持原生的并发和并行编程。 Golang使用了一种叫做“goroutine”的轻量级线程,可以在一个进程中创建成千上万个并发的任务。 Golang还使用了一种叫做“channel”的通信机制,可以让不同的goroutine之间安全地传递数据。 Bun则没有原生的并发和并行编程的支持,只能使用一种叫做“worker”的机制,来创建一些后台的线程。 Bun的worker不能共享内存,只能通过一种叫做“postMessage”的方法来传递数据。 因此,Golang在多线程利用上比Bun更有优势。

综上所述,Bun和Golang在服务器的性能要求上有各自的优势和劣势,没有绝对的好坏之分。具体的选择还要看应用的需求和场景

(1) Is Golang better than bun? - My Million Readers. https://www.mymillionreaders.com/is-golang-better-than-bun/.
(2) Hello world performance: Bun vs Go | Tech Tonic - Medium. https://medium.com/deno-the-complete-reference/hello-world-performance-bun-vs-go-5581004d90e9.
(3) Bun v/s Go: Hello world performance comparison — Part 2 fasthttp - Medium. https://medium.com/deno-the-complete-reference/bun-v-s-go-hello-world-performance-comparison-1f5418945112.
(4) Bun v/s Go: Hello world performance comparison | Tech Tonic - Medium. https://medium.com/deno-the-complete-reference/bun-v-s-go-hello-world-performance-comparison-6d911d0d41bf.
(5) Benchmarking Go vs Node vs Elixir - Stressgrid. https://stressgrid.com/blog/benchmarking_go_vs_node_vs_elixir/.

声明:八零秘林|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - bun 写之前nodejs的接口性能会有提升么?对比golang 的 gin 框架写的接口,使用bun来运行nodejs的接口速度有多少差距


记忆碎片 · 精神拾荒