{"id":7182,"date":"2023-10-30T20:54:27","date_gmt":"2023-10-30T19:54:27","guid":{"rendered":"http:\/\/blog.bart.sk\/en\/?p=7182"},"modified":"2024-01-25T12:39:41","modified_gmt":"2024-01-25T11:39:41","slug":"will-bun-1-0-defeat-unthreatened-nodejs","status":"publish","type":"post","link":"https:\/\/blog.bart.sk\/en\/will-bun-1-0-defeat-unthreatened-nodejs\/","title":{"rendered":"Will Bun 1.0 defeat the unthreatened NodeJS?"},"content":{"rendered":"<p><b>My work day usually begins, in addition to a quick update from my colleagues&#8217; lives, with a short survey of what&#8217;s new in the IT world. One of the topics I&#8217;ve been following over the last few months is the engine\/all-in-one JavaScript runtime &amp; toolkit Bun. Ever since the 0.3 version, it&#8217;s been talked about everywhere as the vanquisher of NodeJS. Is it really true? In my opinion, only partially.<\/b><\/p>\n<h2><b>Bun vs. NodeJS \u2013 what are the differences?<\/b><\/h2>\n<p><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/182802334-d9c42afe-f35d-4a7b-86ea-9985f73f20c3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-11195\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/182802334-d9c42afe-f35d-4a7b-86ea-9985f73f20c3-300x263.png\" alt=\"\" width=\"300\" height=\"263\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Before I start describing Bun, let&#8217;s remember what NodeJS actually is. It&#8217;s an environment that&#8217;s used to create and run modern applications written in JavaScript. It&#8217;s written in C and uses the V8 engine (and all its derivatives) to read and translate and it&#8217;s used in the Chromium browser. Its main advantage is launching concurrent requests.<\/span><\/p>\n<p><b>Bun is basically something like NodeJS.<\/b><span style=\"font-weight: 400;\"> It&#8217;s supposed to serve the same purpose, i.e. to write and translate applications created in JavaScript, but it has more instruments integrated in its base. I&#8217;ll come back to those later.\u00a0<\/span><\/p>\n<p><b>The first difference between Bun and NodeJS is that Bun uses JavaScriptCore as the engine<\/b><span style=\"font-weight: 400;\">, which is used in Safari. At the moment, I can&#8217;t say if it&#8217;s better, as its primary designation is smaller, but it&#8217;ll certainly show in the future.\u00a0<\/span><\/p>\n<p><b>Another significant difference is the use of the Zig language, in which the entire Bun is designed.<\/b><span style=\"font-weight: 400;\"> If you&#8217;ve never heard of Zig, Rust is maybe more familiar. Zig is basically similar to Rust &#8211; designed mainly for security, multi-threaded, modern (but written more traditionally) and fully compatible with C\/C++.<\/span><\/p>\n<h2><b>What extras does Bun have?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">As I mentioned, Bun has some apps and modules built in to help developers streamline their work. I&#8217;ll highlight the ones that are most important to me personally:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><b>TypeScript applications: <\/b><span style=\"font-weight: 400;\">Bun contains the TypeScript transpiler already in the base, so no setup or debugging is necessary. If you have your own tsconfig.json, you can fine-tune the operation of the transpiler thanks to it, but if the normal settings are enough for you, it isn&#8217;t necessary at all. Simply create a file with the .ts or .tsx extension and go ahead.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Development mode: <\/b><span style=\"font-weight: 400;\">Amazingly fast -watch mode that instantly translates any application, and -hot mode for quickly rendering changed parts.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>package.json: <\/b><span style=\"font-weight: 400;\">Full support for package.json file and node_module folder.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Tests: <\/b><span style=\"font-weight: 400;\">Bun supports writing tests already in the base. The syntax is compatible with Jest, it runs fast and there&#8217;s also support for snapshots.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Compatibility: <\/b><span style=\"font-weight: 400;\">Bun is almost completely compatible with applications written for NodeJS. It uses the same JavaScript and the API and a large part of the most commonly used NodeJS commands are also supported. Already there are plenty of tutorials on the Internet on how to migrate or test your apps from NodeJS to Bun. It&#8217;s worth a try.<\/span><\/li>\n<\/ol>\n<figure id=\"attachment_11199\" aria-describedby=\"caption-attachment-11199\" style=\"width: 800px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/1mgzahy7fm60frvvdi41.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11199 size-full aligncenter\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/1mgzahy7fm60frvvdi41.png\" alt=\"\" width=\"800\" height=\"242\" \/><\/a><figcaption id=\"caption-attachment-11199\" class=\"wp-caption-text\"><i><span style=\"font-weight: 400;\">Figure 1: Server configuration. Source: <\/span><\/i><a href=\"https:\/\/dev.to\/mourishitz\/running-nestjs-server-with-bun-4cdl.\"><i><span style=\"font-weight: 400;\">https:\/\/dev.to\/mourishitz\/running-nestjs-server-with-bun-4cdl<\/span><\/i><\/a><\/figcaption><\/figure>\n<figure id=\"attachment_11198\" aria-describedby=\"caption-attachment-11198\" style=\"width: 800px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/lp87mipomtk7xohpai81.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11198 size-full\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/lp87mipomtk7xohpai81.jpg\" alt=\"\" width=\"800\" height=\"632\" \/><\/a><figcaption id=\"caption-attachment-11198\" class=\"wp-caption-text\"><i><span style=\"font-weight: 400;\">Figure 2: Test from the configuration in Figure 1 for development &#8211; almost twice as many requests processed in 30 seconds. Source:\u00a0 <\/span><\/i><a href=\"https:\/\/dev.to\/mourishitz\/running-nestjs-server-with-bun-4cdl\"><i><span style=\"font-weight: 400;\">https:\/\/dev.to\/mourishitz\/running-nestjs-server-with-bun-4cdl<\/span><\/i><\/a><\/figcaption><\/figure>\n<figure id=\"attachment_11197\" aria-describedby=\"caption-attachment-11197\" style=\"width: 800px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/nsj5r7l8g2akes30vpsm.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11197 size-full\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/nsj5r7l8g2akes30vpsm.jpg\" alt=\"\" width=\"800\" height=\"632\" \/><\/a><figcaption id=\"caption-attachment-11197\" class=\"wp-caption-text\"><i><span style=\"font-weight: 400;\">Figure 3: Test from the configuration in Figure 1 for a built project &#8211; almost 3x more requests processed in 30s. Source: <\/span><\/i><a href=\"https:\/\/dev.to\/mourishitz\/running-nestjs-server-with-bun-4cdl\"><i><span style=\"font-weight: 400;\">https:\/\/dev.to\/mourishitz\/running-nestjs-server-with-bun-4cdl<\/span><\/i><\/a><\/figcaption><\/figure>\n<p><b>Comparison by practice<\/b><\/p>\n<p><span style=\"font-weight: 400;\">I won&#8217;t uselessly describe the common tests on the hello-world demo here. I prefer to share my own experience and my feelings tests from practice. I found that:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>Greater performance is noticeable on all fronts<\/b><span style=\"font-weight: 400;\"> \u2013 in application launches, tests and especially in the -watch mode in development. Here I would even describe the difference as brutal \ud83d\ude42<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Already in the base, Bun is prepared for great scalability and parallelism.<\/b><span style=\"font-weight: 400;\"> It can handle 2-3 times more requests than NodeJS in general. However, the question remains as to what the stability will be.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Due to reduced code and optimization of RAM and performance, <\/span><b>BUN doesn&#8217;t have a large variance on<\/b> <b>latency<\/b><span style=\"font-weight: 400;\"> (the delay between the user&#8217;s action and the web application&#8217;s response to that action). It ranges somewhere between 0.5-10s, while with NodeJS these values are around 0.05-100s.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Bun uses slightly more RAM, but less CPU than NodeJS.<\/b><span style=\"font-weight: 400;\"> This may be a stumbling block, but there&#8217;s still room for further optimizations.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Many large projects already have their test environments or test versions compatible with both NodeJS and Bun. So you practically just have to choose.<\/span><\/p>\n<figure id=\"attachment_11196\" aria-describedby=\"caption-attachment-11196\" style=\"width: 1000px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/cpu.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11196 size-full\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/cpu.jpg\" alt=\"\" width=\"1000\" height=\"600\" \/><\/a><figcaption id=\"caption-attachment-11196\" class=\"wp-caption-text\"><i><span style=\"font-weight: 400;\">CPU usage for 200 parallel hello world requests. Source: <\/span><\/i><a href=\"https:\/\/medium.com\/deno-the-complete-reference\/hello-world-performance-bun-vs-node-js-2c9d383b94c1\"><i><span style=\"font-weight: 400;\">https:\/\/medium.com\/deno-the-complete-reference\/hello-world-performance-bun-vs-node-js-2c9d383b94c1<\/span><\/i><\/a><\/figcaption><\/figure>\n<figure id=\"attachment_11194\" aria-describedby=\"caption-attachment-11194\" style=\"width: 1000px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/ram.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11194 size-full\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/10\/ram.jpg\" alt=\"\" width=\"1000\" height=\"600\" \/><\/a><figcaption id=\"caption-attachment-11194\" class=\"wp-caption-text\"><i><span style=\"font-weight: 400;\">RAM usage for 200 parallel hello world requests. Source: <\/span><\/i><a href=\"https:\/\/medium.com\/deno-the-complete-reference\/hello-world-performance-bun-vs-node-js-2c9d383b94c1\"><i><span style=\"font-weight: 400;\">https:\/\/medium.com\/deno-the-complete-reference\/hello-world-performance-bun-vs-node-js-2c9d383b94c1<\/span><\/i><\/a><\/figcaption><\/figure>\n<p><b>Could there be a catch?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">I see the problem mainly in the fact that <\/span><b>Bun is still new<\/b><span style=\"font-weight: 400;\">. In the IT world, you often encounter exaggerated enthusiasm for something new and revolutionary, but over time it often fades into dust because of a security problem or because the developers couldn\u2019t be bothered to improve their creation further.<\/span><\/p>\n<p><b>Currently, Bun&#8217;s greatest strength is that it&#8217;s a small library thanks to which it can work super fast.<\/b><span style=\"font-weight: 400;\"> But this is only version 1.0. In contrast, NodeJS has already gone through 21 large cycles of repairs, improvements and most importantly, constant listening to the community and responding to its requests. Bun doesn&#8217;t have that yet &#8211; years of experience, failures and mistakes. I wouldn&#8217;t like to see this hopeful library gradually slow down due to redundancies or the stacking of fixes.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Who knows, maybe in the future there&#8217;ll be another &#8220;vanquisher&#8221; that&#8217;ll stand against both Bun and NodeJS. <\/span><b>Nevertheless, I think that the &#8220;good old&#8221; NodeJS will still be with us in 10 years.<\/b><span style=\"font-weight: 400;\"> After all, even PHP has succumbed to clinical death several times, but one successful restart was enough and the developer community is returning to it again.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"My work day usually begins, in addition to a quick update from my colleagues&#8217; lives, with a short&hellip;","protected":false},"author":17,"featured_media":7297,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","csco_display_header_overlay":false,"csco_singular_sidebar":"","csco_page_header_type":""},"categories":[199],"tags":[491,312,183,492],"_links":{"self":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/7182"}],"collection":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/comments?post=7182"}],"version-history":[{"count":1,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/7182\/revisions"}],"predecessor-version":[{"id":7183,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/7182\/revisions\/7183"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/media\/7297"}],"wp:attachment":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/media?parent=7182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/categories?post=7182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/tags?post=7182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}