Kill Node.js! The new JS runtime is "quick to fly"!

Before we get started, let’s briefly explain what the JavaScript runtime is and why we should care about its speed.

Imagine you write a story in JavaScript and you need someone to read it out loud. The JavaScript runtime acts like a friendly "narrator" and brings your story to life! It is the special environment in which JavaScript stories are read and executed. Technically speaking, this "narrator" consists of components such as the JavaScript engine, which is the core of the runtime and is responsible for understanding and running code.

Bun is a brand new JavaScript runtime. On September 8, Bun1.0 was officially released, marking that it has reached a stable production-ready state.

Bun's developers say it's positioned as a drop-in replacement for Node.js and aims to eliminate complexity and slowness, simplifying JavaScript development by eliminating "layers of tooling."

1. Challenge Node.js: integrating runtime, packaging tools and testing framework

"Bun is a complete toolkit for building, running tests, and debugging JavaScript and TypeScript, from a small script to a complete server-side rendering application," said creator Jarred Sumner in a video demonstration.

Unlike Node.js, Bun uses Apple's JavaScriptCore engine and is built with Zig. It is designed to make applications faster without adding additional complexity to your code. According to the Bun team, its main advantages are:

  • When using Bun, you don't need node, it has built-in monitoring mode. Bun makes Node.js tools (including node, npx, nodemon, dotenv or cross-env) unnecessary.
  • Bun can run different files like .js, .ts, .cjs, .mjs, .jsx and .tsx files which can replace transpilers like tsc and babel.
  • Bun is a Jest-compatible test runner that supports snapshot testing, code coverage and mocking. So you can write unit tests without installing additional dependencies.
  • Bun is also a JavaScript bundler with "best-in-class" performance and an esbuild-compatible plugin API.
  • Bun is also an npm-compatible package manager, with all the familiar commands that yarn and npm have.
  • Supports both EcmaScript modules and CommonJS module system.

Although its claim to be a "replacement" has also been questioned by developers, it can be seen from the above advantages that Bun does try to integrate runtime, packaging tools, testing framework and other functions into one tool, thereby solving tool fragmentation This leads to problems such as low development efficiency.

However, the 1.0 version officially released by Bun is still different from the previous beta version. Developer Jarred Sumner shared on Twitter that one of the hardest things about releasing Bun 1.0 was the removal of the front-end server that was part of the beta.

“I wish we had more time to make Bun more geared towards front-end development,” Sumner said in a Twitter Q&A after Thursday’s launch. “That’s not a bad thing for it — you can use the tools you already use. . But I still think integrating the runtime directly with front-end build tools is a huge opportunity."

He noted that user feedback indicated there was no problem with removing the front-end servers, and that most reactions to the news on social media were positive. A day after its release, the buzz around Bun was all about its speed and ease of use.

2. Born for speed, but not just speed.

The most impressive thing about Bun is its speed. The reason why it dares to challenge Node.js is its greatest confidence.

According to the introduction of the Bun team in the live broadcast, Bun's writing speed is three times that of Node.js, and its file reading speed is three times that of Node.js. Bun installs packages 29 times faster than npm; in JavaScript tests, it is 13 times faster than Jest; and the packaging speed is 1.75 times faster than esbuild.

Ashcon Partovi, the product manager of Oven, the development company to which Bun belongs, specifically mentioned that Bun can replace any npm run command with Bun run. It takes npm about 150 milliseconds to start running the script on the MacBook Pro, which is in sharp contrast to Bun's 30 milliseconds. . "Npm feels significantly lagging. Bun feels instantaneous."

Bun's competitors are Node.js and Rust-based Deno. Software engineer James Cornick specifically compared the three runtimes Bun, Node.js and Deno, and the results showed that Bun's performance was better than the latter two.

"Powered by Zig, Bun aims to be an all-in-one runtime and toolkit with a focus on speed, packaging, testing and compatibility with Node.js packages," Konik wrote. "Its biggest draw One of the strengths is its performance, it's significantly faster than both Node.js and Deno."

He noted that the Bun maintainer provided an example benchmark running an HTTP handler that uses React to render a server-side page. Bun handles approximately 68,000 requests per second, while Deno and Node.js handle 29,000 and 14,000 requests respectively.

In his own testing of early versions of Bun, Konik found that Node.js handled an average of 21.29 queries per second, while Deno's averaged 43.50. Bun handles an average of 81.37 queries per second.

At the same time, Konik also found that in another comparison between Node.js, Deno and Bun, Bun was the fastest in handling concurrent connections, and the number of requests per second was also quite high, for example, in the case of 10 concurrent connections Below, Bun can achieve 110,000 requests per second, while Node.js can achieve 60,000 requests per second, and Deno can achieve 67,000 requests per second.

Of course, speed isn't the only factor to consider when running.

Developer markthree also pointed out that each runtime has its advantages: "Bun is more performance-focused, so in terms of current performance, it is much better than the other two runtimes. Deno is synonymous with security, as far as I'm concerned. I can safely use packages from the community without worrying about them doing things to my system that I don't know about. Node is also starting to push hard on performance and security now. All in all, competition is a good thing and helps advance the JS runtime. evolution."

3. The Linux and macOS versions are ready, but the Windows version is still "experimental"

Generally speaking, many people still welcome the fledgling Bun.

Because Node-based development environments often involve a collection of different tools, they are difficult to manage, and both Bun and Deno simplify this. Bun is geared more toward compatibility with Node.js and CommonJS than Deno, which is a plus for many developers.

However, Bun still has a lot of room for improvement. For example, the Bun team provides production-ready local versions for macOS and Linux, but the team is still working on getting the Windows version to work properly. At this conference, the version of Bun for Windows was called "highly experimental." So far, Bun only works on Mac, Linux, and Windows Subsystem for Linux (WSL).

According to the documentation, the Windows version currently only supports the Bun runtime, and "the package manager, test runner, and packager have been disabled until they become more stable."

4. Should you switch to Bun instead of Node.js?

As a new generation in the JavaScript world, Bun shows an interesting side: it has some neat built-in features that Node.js doesn’t have, and it’s really fast. If you're thinking about what to use for your next project, it's not a bad idea to give Bun a try.

As for how to choose among several JS runtimes, developer Shalini Tewari's views are quite representative.

"Node.js, Bun, and Deno are all server-side js runtimes, but their goals are completely different. The choice between Bun and Node.js depends on your project needs."

"If you need speed and a simple, lightweight experience, go with Bun. If you want a broader ecosystem and community support, then Node.js is a solid choice. You can even use both Methods to make your JavaScript applications powerful and efficient."

Guess you like

Origin blog.csdn.net/Z__7Gk/article/details/132977929