An Introduction to the Bun JavaScript Runtime — SitePoint


A rival JavaScript runtime contender has entered the battle between Node.js and Deno. In this article, we take a first look at Bun, and the reasons it may tempt you away from your current favorite.

Ryan Dahl released Node.js in 2009. It wasn’t the first server-side JavaScript runtime, but Node.js rapidly gained momentum. Version 20 arrived in 2023, and Node.js has the largest development ecosystem, with 3.2 million modules — accounting for almost 500 billion downloads per week (according to npmjs.com).

In 2020, Ryan Dahl released Deno — a remix of “noDe” — to modernize JavaScript development and address legacy issues with Node.js security, API compatibility, tooling, and module management. Reception has been positive, although Deno is yet to challenge Node’s domination.

In 2022, Jarred Sumner released Bun following his frustrations with the speed of Node.js when developing a Next.js project. The origin of the name is unclear, and the logo doesn’t help! It could relate to the food, fluffy rabbits, “bundle”, or perhaps it’s a short, memorable name and the bun.sh domain was available.

The intention: Bun will become a drop-in replacement for Node.js, Deno, serverless JavaScript, and tools such as webpack, Babel, and Yarn. Rather than running npm start to launch your Node script, you can run bun start and take advantage of Bun’s speed.

Tasty Bun Benefits

Node.js and Deno use Chrome’s V8 JavaScript engine. Bun opts for the JavaScriptCore engine which powers WebKit browsers such as Safari. Bun itself is written in Zig — a low-level programming language with manual memory management and native threading to handle concurrency. The result is a lightweight runtime with a smaller memory footprint, quicker start-up times, and performance which can be four times faster than Node.js and Deno under certain (benchmarking) conditions.

Like Deno, Bun has native support for both JavaScript and TypeScript without requiring a third-party transpiler or configuration. It also supports .jsx and .tsx files to convert HTML-like markup to native JavaScript. Experimental support for running WebAssembly-compiled .wasm files is available.

Internally, Bun uses ES Modules, supports top-level await, translates CommonJS, and implements Node’s node_modules resolution algorithm. Bun caches modules in ~/.bun/install/cache/ and uses hardlinks to copy them into a project’s node_modules directory. All projects on your system will therefore reference a single instance of the same library, which reduces diskspace requirements and improves installation performance. (Note that macOS installations retain local versions for speed.)

Bun supports Node’s package.json, npm equivalent commands, and bunx — a npx-like option to auto-install and run packages in a single command. For example:

bunx cowsay "Hello, world!"

bun init scaffolds empty projects in the same way as npm init, but you can also template a new project with bun create