488

July 25th, 2022 × #JavaScript#Web Development#Performance

What is Bun? The New JS Runtime

This episode covers Bun, a new JavaScript runtime focused on performance and batteries included tools like a bundler and transpiler.

or
Topic 0 00:00

Transcript

Announcer

Monday. Monday. Monday.

Announcer

Open wide dev fans, get ready to stuff your face with JavaScript, CSS, node modules, barbecue tips, get workflows, breakdancing, soft skill, web development, the hastiest, the craziest, the tastiest web development treats coming again hot. Here is Wes Barracuda Boss and Scott CSD.

Topic 1 00:27

Introducing Bun

Scott Tolinski

Welcome to Syntax. In this Monday, hasty treat, we're gonna be talking about bun, b u n. Bun is the latest and greatest in the JavaScript world. It's a new JS runtime and more. We're gonna be talking all about what BUN is and why you should have it on your radar.

Wes Bos

Very excited about bun here. I I like all things buns, ham buns. Yeah. Buns buns. Yeah. Buns buns. And the button, I like it. JavaScript environment, you name it. So bunnies, pretty exciting. Like bunnies. Bunnies. I do like bunnies. Yeah. Yep.

Topic 2 01:08

Sponsors

Scott Tolinski

Gotta love all that stuff. It. This episode is sponsored by 2 amazing companies.

Scott Tolinski

LightStep Incident Response is the intelligent on call scheduling and escalation, as well as FreshBooks, Books, the amazing cloud accounting software that you know and love. Let's talk about one of our sponsors, FreshBooks.

Wes Bos

CS. FreshBooks is the cloud accounting service for your small business.

Wes Bos

You're gonna wanna use this if you are running a small business, a large business. You're just sending out a couple invoices. This is the one that you wanna do. You're gonna get paid faster.

Wes Bos

My clients love FreshBooks because they're able to just log in And see it. Like, I have people send me invoices via FreshBooks, and it's great because I can just log in and see exactly how much I owe, What's been paid? What's not been paid? There's no hemming and hawing. You can pay right online. You can hook it up with their payment processor. You can hook it up with Stripe or PayPal.

Wes Bos

You can send late payment reminders. You name it, they got it. Check it out. Freshbooks.comforward/syntax.

Wes Bos

You're gonna get paid faster. 30 day free trial. Thank you FreshBooks for sponsoring.

Scott Tolinski

This episode is also sponsored today by LightStep incident response. The all in one incident response form for DevOps and SREs.

Scott Tolinski

LightStep instant response is built on the ServiceNow platform, which is used by over 6,000 companies.

Scott Tolinski

It. You pay for the services you actually use, not the number of the people on your team, which is important because you can scale your incident response team without adding to your bill. You get intelligent on call scheduling and escalations.

Scott Tolinski

You can get the full context on your service health, active alerts, and who's on call. LightStep incident response immediately pinpoints issues and groups alerts with machine learning, reducing your time to respond. And this means less noise and more personalization for precise notifications where you can take control on declaring incidents automatically notifying the right teams. And you can get unified incident response by seamlessly orchestrating alerts and incident triage with on call scheduling across Slack, Teams, Zoom, desktop, and mobile. So what you'll want to do is head on over to lightstep.comforward/syntax, and you'll get a free 30 day trial.

Scott Tolinski

Listeners of Syntax will also receive a free LightStep incident response t shirt after firing an alert or incident. So check it out. Are you in need of incident response? Check out lightstep.comforward/syntax.

Scott Tolinski

So, Wes, let's get into BUN.

Topic 3 03:32

What is Bun?

Scott Tolinski

What is BUN?

Wes Bos

Yeah. So we talked about this A couple times on the podcast here or there, and, recently, it was just, like, released as well, was it 1.0 or 0.1, Which means that it's, like, kind of the 1st version out there, that people can try. And it's a new JavaScript environment To focus on, starting fast. This has an edge in mind. New levels of performance of going fast as heck, And just being like a a fully complete tool. So it's it is aiming to be on the same level as Node and Deno, But also be a package manager, a TypeScript transpiler, a bundler. It looks like it even has test.

Wes Bos

A test runner, included, which also Node just rolled that out as well. So it's kinda interesting because For the longest time, we've had Node as our JavaScript environment. And then a couple of years ago, we got Deno, which is sort of A different JavaScript environment. It still runs JavaScript, but the API is slightly different.

Topic 4 04:22

Comparing Bun to Node and Deno

Wes Bos

Flare has its own runtime.

Wes Bos

We talked about who else has their own runtime? Bloomberg folks have their own runtime. We did a whole show on So now there's a new one called Bun, and it's looking really, really interesting.

Wes Bos

It's kind of cool because They're focused on performance, but they are also, like, trying to support the whole node API so that should you need to use any packages That, are only node only. It it will work. So I don't think it's necessarily going to try to replace node, But I think it's going to try as we start to move towards more serverless and edge functions, having a Script environment that runs extremely fast and starts extremely fast.

Wes Bos

That cold start is is really important, Is something that people are going to be looking for, so I am pretty

Topic 5 05:34

Bun focuses on performance

Scott Tolinski

excited to see that this is coming out. Yeah. I'm excited too because, like, we we haven't really talked about a whole ton, but, you know, so many other languages have all of these features built in that Node. Js for the longest time just has not had built in, and then you go to land for these things. The next thing you know, there's 8 different versions of them available.

Topic 6 06:04

Bun has batteries included

Scott Tolinski

So, you know, with this, you'll have better TypeScript integration because It's it's just going to work, which you gotta love that. Right? You gotta love just being able to use TypeScript.

Scott Tolinski

You'll have bundling baked in. You'll have all that task runner stuff baked in the stuff that we have a note with package. Json. But then they they also promised, you know, the 20% faster package installs Via the same, NPM compatible anything that you know and love. I'll I'll be interested to see how that compares to, like, CSPM, or how the workspaces and monorepo stuff exists inside of BUN. But so far, it really feels like if you go down the list of all of the things that it is, It's kind of, an all encompassing.

Scott Tolinski

You could say it's like a steamed bun wrapped around the entire JavaScript ecosystem.

Topic 7 06:47

Bun implements common APIs

Wes Bos

Yeah. And, this this just looks really good because it takes a lot of the, like, Web APIs as well. And so we talked about this with Deno as well where they take a fetch, WebSocket, readable stream.

Wes Bos

All of these are standard JavaScript APIs, and they just include them in the language itself. And for the long This time, we had browser APIs and then we had node APIs and things like fetch didn't exist in node.

Wes Bos

And now we can use them Right across the board without having to do any polyfills, other things like the dotenv just works.

Wes Bos

So, like, pretty much every application that you build a node needs environmental variables. And if you have a .env file, you have to Like, do the require dotenv and run it right.

Wes Bos

And this is just built right into BUN. So Some of those just like easy, easy to use things. Dino had this as well where it's like, oh, yeah. Like all of us are doing this. Lots of us are using TypeScript. Lots of us are using JSX.

Wes Bos

Why doesn't it just work? You know, I don't wanna have to do all this sort of overhead. And it's interesting that that just is built right in. They say it's batteries included. Yeah. Batteries included is something that I I like to hear more and more.

Scott Tolinski

The older the older I get and the more things that I build, I don't want to have to build everything from scratch all the time, and I don't wanna have to pick all of these things. Now one of the interesting things they say, why you know, why is it so fast? And the answer is that they just built in Zig.

Topic 8 08:17

Bun builds on Zig language

Scott Tolinski

Zig It is it's a language that you may not have heard of. It's its own programming language. It's it's a general purpose language, And a tool chain for maintaining robust, optimal, and reusable software. And Zig is kind of its Own interesting topic if we wanted to dive into Zig sometime, but Zig is a it's a tone language. And, you know, this is something before it. I had before I learned about BUN, I would assume that it's it's kind of similar to most people. But before I'd I'd heard of BUN, I had not heard of Zig. And then BUN came out, And then all of a sudden, I'm I'm sure once bun hit Zig's homepage got a ton of traffic because it's it's a newer language, and it's a smaller community.

Scott Tolinski

But it's supposed to be very, very fast, and it's supposed to be I don't know. It seems seems like, as far as These types of languages go whether you have, like, a Rust or or, you know, c or or Java or any of that stuff. It seems like it It's way more readable than many of those languages that I've tried to read before. It does feel JavaScript esque, dare I say, in some ways. Yeah.

Topic 9 09:36

Bun supports web server out of the box

Wes Bos

One neat thing that Bun has out of the box is the ability to run a web server, and that is something that does exist with, with Node as well. However, in most cases, people don't just use the built in HTTP dot create server in Node. They usually reach for Express or one of those connect servers. So because we now have web standards that are request and response, those things have been standardized, and they work in Deno, and now they also work in Bun. It's kind of cool because you can just make a file, and right from that, you can export a config object. And if That config object has a fetch request on it.

Wes Bos

That will be how you handle it. So if you've ever used a serverless function or a Cloudflare Worker or any of these different varieties is very much similar to like that. So you want to Just quickly whip up a quick little script that has a web server in it.

Wes Bos

You can just export default curly brackets, Specify the port that you want. Specify a fetch method. And then inside of that fetch method, you can create you get the request, And you can return a response and you can do literally anything else you want inside of there asynchronously.

Wes Bos

Yeah. Or yeah. Asynchronously. Yeah. Okay.

Topic 10 10:58

Bun supports async code

Scott Tolinski

I was like, did I get the right one? I I think so. You know, that's it's so funny without that word. It it it's always Like, I'll I'll just trust. I'll default to whatever whatever you said at the time because I don't wanna have to think about it.

Scott Tolinski

I so, you know, We've talked a little bit about what what this thing is and and maybe, like, why you might wanna use it. But, like, can you use it? I guess this is a whole another topic. So, like, can you use this thing today? And the answer is really yes. It's it's not I don't wanna say it's It's not done or anything like that. There's certainly we'll talk a little bit about what's not implemented and what the road map is. But as far as installing and getting it to try to use this thing, you could It's all bun today and basically attempts to replace your node command with the bun command, and it should run your thing. So instead of saying, like, node index dot j s, you would just say bun index dot j s, and that will run bun. Now I'm under of the understanding that it should mostly just work. There are some APIs that aren't implemented yet, which we'll talk about. But I did even see, You know, there's already a a SvelteKit adapter for BUN. There's people. There's a whole section on using Next. Js with BUN in BUN's documentation.

Scott Tolinski

So, I mean, I think it seems like from their intention, you should be able to start, like, applying this to real projects and seeing essentially free games. And and you you gotta love free games from doing nothing really other than just changing a word. In fact, you're you're saving some time because the word bun is shorter than the word node. So you're gonna say 1 letter typing, and you're gonna get speed games on top of that. Yeah. So I'm just reading it here. It supports the

Wes Bos

Node API. They call it NAPI, and that is for Any modules that are native, meaning that there's lots of modules in Node land that are not written in JavaScript. They're written in c, or c plus plus or something, and they're compiled.

Wes Bos

In in order to surface the functionality of those modules Like, they could even be written in Dart or something like that. So to surface that to Node, they have this thing called the Node API, the n API, And that is fully supported, meaning that any of these modules that are native in nodelan will work in BUN. And then you also have, like node native APIs, which is like the FS API, the path API, probably crypto, Although there's web crypto, so probably not.

Wes Bos

It supports FS and Path right now, and then other APIs Like buffer process, those are not implemented just yet. And the same thing with Deno is that over time, we saw them, Like sort of polyfill them, I guess, so that if you want to use like, oh, I would love to move it upon, but We're using this one like Chihuahua JS, and that uses us internally, it uses a node a native API, and we we just can't We don't have time to refactor that out.

Wes Bos

Eventually, they will be all supported, so you'll be able to bring those things in. And what I The sort of what I see for the ecosystem in general is long term.

Wes Bos

A lot of these modules will be built not just as node modules, but they'll be built as Server JavaScript modules, and they'll be they'll be built in such a way that you can at least you can run them in all the different environments ideally. That's what I see happening. Yeah. It's wild how, how this landscape has changed. I mean, for so long, we had JavaScript or Node, and then we had

Scott Tolinski

IO briefly.

Scott Tolinski

But other than that, like, you know, until more recently, we haven't really had a lot of different server node run or JavaScript runtime. So It is interesting how this world's kind of changing a little bit now, and we are seeing, different different characters pop up and play here.

Scott Tolinski

It. You know, I think it's interesting that, I I saw that this supports CommonJS as well, CommonJS and text as well as ESM. So you're not gonna have to worry about, is this just, like, the newer features? It seems like like you mentioned a lot of stuff.

Scott Tolinski

I'm interested about, like, the section in their docs that say, it. Like, what's not implemented yet? Have you checked that out at all, Wes? No. I haven't. What what do they have there? So not implemented yet. Web streams via the fetch API, web streams via HTML Rewriter, web, the socket server, package hoisting that matches NPM's behavior, source maps, JavaScript minifier, CSS minifier, CSS parser, tree shaking, for both CSS and JavaScript extends in TypeScript TS config dot JSON, so that might be a problem with your SvelteKit sites. You need that. Well, you don't need that, but they use TS, it.

Topic 11 15:13

APIs not yet implemented in Bun

Scott Tolinski

Extends, TypeScript decorators, j s s JSX pragma. I don't know what JSX pragma is, but it sounds kind of like a Love a monster or something. Rag.

Wes Bos

It's interesting how Yeah.

Wes Bos

You just said HTML Rewriter, they don't support yet. And I just was like, hey. Isn't that that's a CloudFlare Worker thing. Web streams with HTML Rewriter. Yeah. Just streams. Yeah. Okay. Because HTML rewriters are pretty cool Cloudflare thing where you can intercept a request and as you are streaming it in, You can rewrite it. So you could you could do something as as silly as replace Scott with Scotty or, or or every image Automatically add a alt tag or any. You can do literally anything you want as it's streaming in. I always thought that was a really cool API, And maybe that signals that they will be implementing the HTML Readerwriter API, In button, which is which would be really sweet. Nice. Yeah. That'd be pretty cool.

Scott Tolinski

There's also a config file, of course, because we gotta love config files. There is an interesting enough. Did you notice, that the like, their bundler config file is a TOML file? No. No. I didn't. Yeah. So not YAML, but Tomo. Tomo.

Topic 12 17:10

Bun uses TOML for config

Scott Tolinski

Yeah. We had Tom on the show, the creator of Tomo. Tom's obvious minimal language.

Wes Bos

And it is. It's

Scott Tolinski

it's nice. I like TOML, so it's kind of interesting.

Wes Bos

Yeah. It's I'm curious that they didn't choose JSON five For the config where, like because of one of the problems with, package. Json is that it's a regular JSON, and you can't do things like comments and you have to have a trailing comma and and whatnot. So it's interesting that they did not just go for JSON five, which Like, Versus Code went all in on JSON five, where any time you are writing a JSON config in Versus Code, you can do comments and and whatnot. But I wonder if there's features of TOML that are not available in JSON or if, they just don't like JSON. I think it's just nice.

Scott Tolinski

Yes. Yeah. He's a fan of it. This equals that. This equals that. And here's some brackets. Yeah. Don't have to worry about not having comments like you mentioned. But, Yeah. I think it's just nice. There's also a road map here that will link into the show notes so you can kind of see where they're going. They have this over in the issues, but it's an it. It's a road map. I I love road maps, especially when they're they have checkboxes because this says 17 of a 133 tasks complete.

Topic 13 18:27

Bun has a public roadmap

Scott Tolinski

Gotta love that.

Scott Tolinski

You can see exactly what's complete, what's not complete, what's coming down the line, what's not, where they're at with many of these things because there is a whole lot of stuff. Like, for instance, the web compatibility section, you can get exactly into you can see when that support for HTML rewrite is coming or any of that stuff. So there is a lot a lot a lot here. Oh, here's Node. Js, compatibility.

Scott Tolinski

Crypto polyfill should be used Should use hard so it seems like they have a crypto polyfill west. Crypto polyfill should use hardware accelerator, Hardware accelerated crypto.

Scott Tolinski

So it seems like if they're referencing the crypto polyfill, then they probably have a polyfill for it. Yeah. Yeah.

Wes Bos

One other thing they have here is they have, an SQL SQLite, Implementing What do you call that? A driver or ORM? Escalate is pretty cool. You can essentially have a database just as a file on your system, and you can you can just keep that file in in literally anywhere. And SQLite is very, very popular in, like, desktop applications because if your your desktop app wants to store some data, You don't need to go to an external server. You don't want to run a, like a database server locally. You can just have an SQLite a file in your folder and you can save and whatnot. And it's pretty cool. I'm a big fan of Escuelite, and it looks like they have written their own. I think it's called the ORM, and apparently is very fast. So I'm wondering, like, they're doing everything here. TypeScript compiling JSX JavaScript runtime.

Wes Bos

Like, this seems like the whole batteries included, except for the, like, obvious parts of, like, authentication and image processing. That's more That's more to a framework than an environment. Yeah.

Scott Tolinski

This is a big old project, and I think it's one we're gonna be seeing a lot of for the time to come here. So here's a question is

Topic 14 20:23

Why do we need another JS runtime?

Wes Bos

and we kind of answered this, but, like, maybe people are asking, like, look, why do we need another JavaScript environment? Isn't this just Like like, it seems like we're getting excited about incompatibility across multiple. Do you have any thoughts on that? I think,

Scott Tolinski

I think it's just Speed, man. I think it's both I I think it's just both an opportunity to, have something that's fast here, And it solves a bunch of the annoyances. I I would imagine, when Jared created Bun, I should say this is I don't know if we we if you mentioned that this was created by Jared's summoner.

Scott Tolinski

And so I would imagine Jared Jared Seems like his influence for this was just kind of frustration with the current platform.

Scott Tolinski

And what we're seeing here is more tools that promise to do more things In more parts of the platform, whether that is, other tools like we have Roam. That is one of those ones that's out there that wants to do everything as well. Yeah. Like, it seems like We want we want the community and the type of support that other languages have had for so long. You know, there's something so nice about Rust having a built in package manager and test runner and bundler and all that stuff. You don't have to worry about it. I don't have to I don't have to be changing out which bundler I'm using every single month just to get some big perf gains or something. So It seems to me like this is just a a way to step forward and a way to kind of fill in some of the gaps and and take control over your entire application build And at the same time, get some sick perf gains. Yeah. If anything, this is like, what a vote of confidence for JavaScript

Wes Bos

on the server in general because In the browser, you have to use JavaScript.

Wes Bos

On the server, you can use literally anything you want. And when when they first rolled out Node. Js, people People are laughing like belly rolling.

Wes Bos

Like, why would you want to use JavaScript on the server? What a joke.

Wes Bos

And here we are, what, like 12, 13 years later, and we're starting to see multiple JavaScript environments. And the way to code for the Edge is in JavaScript.

Wes Bos

Most serverless functions are done in JavaScript. So huge vote of confidence for JavaScript. And If anything, then you might be saying, oh, my gosh. Another thing. This is a huge vote of confidence both for web standards. It's implementing standard stuff. They're not rolling out their own APIs.

Wes Bos

And it's a vote of comments for JavaScript in general, so always bet on JS.

Scott Tolinski

The always bet on JS. Yeah. That is very true. Well, this has been BUN.

Scott Tolinski

If you wanna check it out, we'll have some links for you, Ben. It's a good Ben. If you wanna check it out, check out those links in the show notes, And, give it a try. I I'm personally gonna be giving this a try. At least nothing nothing else. Just, you know, firing up a basic HTTP server and saying, alright. I did it. That's pretty cool. And, yeah, give it a try. So that's it for Bun. We will see you on Wednesday it for, I believe, a potluck episode. Get ready.

Topic 15 23:06

Conclusion and call to try Bun

Wes Bos

Catch you then. Peace. Peace.

Scott Tolinski

Head on over to syntax.fm for a full archive of all of our shows, and don't forget to subscribe in your podcast player or drop a review if you like this show.

Share