6 ms·
Bun is much faster than Node.js 22 at decoding Base64 but both rely on same lib
- apatheticonion 2y agoI am working on a tooling that needs to support JavaScript plugins with support for the Nodejs standard library & ecosystem. Nodejs is nearly impossible to embed. Deno is difficult to embed and error prone due to difference with Nodejs. Bun doesn't appear to have an embedding API but I'd certainly give it a crack if they offered a turn key integration.
- veeti 2y agoI'll bite: has base64 decoding ever been a bottleneck for anyone in Node.js?
- mjburgess 2y agoIt would be useful to understand how Bun avoids the same problem with it's JS engine... my guess is that it provides this API directly, via a zig implementation, and leaves the JS engine completely out of it. If so, then node can take exactly the same strategy.
- brabel 2y agoIn Zig, strings are always arrays of bytes [1]. It's the functions that work on the bytes that need to know about encoding... so for base64 they just use the bytes directly: https://github.com/oven-sh/bun/blob/main/src/base64/base64.zig#L17 https://github.com/oven-sh/bun/blob/main/src/base64/base64.z... [1] https://ziglang.org/documentation/master/#String-Literals-and-Unicode-Code-Point-Literals https://ziglang.org/documentation/master/#String-Literals-an...
- colejohnson66 2y agoSo Zig took the C/C++ approach to strings?
- account42 2y agoIt's the most sensible approach, especially for a low-level language. By the point you start caring about the meaning of the string contents you almost always need to deal with grapheme clusters and a whole lot more Unicode bs. Meanwhile many use cases only care about passing the string along or concatenating or replacing substrings, all of which can be done at the byte level with a sensible encoding like UTF-8. Codepoint-level string abstractions in particular are complete nonsense that only serve to give you the illusion of making things easier before learn the hard way that Unicode is more complicated than that. This also goes for UTF-16 which is only a cope extension uf UCS-2 for those that already made this mistake before additionally realizing that 2 bytes are not enough to encode all human languages. Now you might think that declaing all your strings are UTF-8 wouldn't have any of these problems and is the way to go .. until you find out that there are strings you can't represent as (valid) UTF-8 including things that are almost UTF-8 like filenames and other OS-provided data under most POSIX operating systems. This also applies to UTF-16 under Windows btw.
- colejohnson66 2y agoSure, it's a low-level language, but my concern is that it doesn't even enforce the encoding? Yes, UTF-8 source code prevents string literals from being invalid, but any input from users/sockets/files could be invalid UTF-8. Basically, there's no distinction between a "string" with a known length and a byte array with a known length — the same mistake C and C++ have. Conceptually, strings and byte arrays are different things, even if they can be represented the same way, and a type system could enforce that. And yes, UTF-16, used by Java and C#/.NET is a pain-point as it forces conversion from sockets/files from UTF-8 into UTF-16 so they can be used, then another one when writing back. But that's beside the point when talking about Zig.
- brabel 2y agoOf course it doesn't enforce encoding, it needs to be able to handle invalid data, bad encoding etc. if it wants to be a system language. You can trivially enforce it's valid utf-8 if that's what your application requires by using the `@import("std").unicode` package.
- furstenheim 2y agoCurious, does that affect the complexity of string concatenation? As far as I remember V8 "uses" ropes, so string concatenation was constant time. Not O(n) like java. Which saves a lot of headaches
- DarkNova6 2y ago> If so, then node can take exactly the same strategy. That's bun in a nutshell. Node could be faster, but they are not. Bun is essentially using the same architecture as Node, just implemented in a more efficient way. Therefore, the hopes that Bun would herald a breakthrough in JS performance are ill informed. However, best case-scenario Node gets a good nudge to become more efficient.
- sharas- 2y agoThe efficient version of Node is called Deno. Bun is copying Deno.
- Jarred 2y agoThe JS engine is impossible to leave out, as this is a JavaScript API and strings are very engine specific. We spend many hours reading JavaScriptCore (the engine)’s code and when we wrote the code for Buffer and related APIs we spent a lot of time benchmarking and iterating on different approaches. A lot of performance work looks like this
- deleted 2y ago[deleted]
- cranberryturkey 2y ago[flagged]
- sebmellen 2y agoI have been incredibly impressed with Bun after using it in some smaller projects. Compared to Deno, it feels like a true alternative to Node.js. It’s so nice to have a TS-native runtime.
- Waterluvian 2y agoIs it native in any significant way or is it just also transpiling but hiding that step enough to make it feel native? I guess this really hinges on how we define native. I really want a runtime that actually leverages the type hints for JIT optimization.
- mrighele 2y agoBun uses JavaScriptCore (the js engine of Safari) so I doubt they are doing anything fancy with typescript types.
- dgb23 2y agoThere are certainly cases where type hints could be leveraged. But ultimately the way a JIT like Turbofan works is that it will do better optimizations if your JS code is already monomorphic (functions signatures, object fields and so on). Now TS sort of nudges you that way if you write simple TS code. But due to structural typing, expressive type golf features and the fact that TS is generally unsound I doubt it would be easy to leverage type hints much beyond that. Here are two talks that are very much related and give some insights into how difficult these kinds of problems are: https://thestrangeloop.com/2022/of-javascript-ahead-of-time-compilation-performance.html https://thestrangeloop.com/2022/of-javascript-ahead-of-time-... https://www.thestrangeloop.com/2019/typing-the-untyped-soundness-in-gradual-type-systems.html https://www.thestrangeloop.com/2019/typing-the-untyped-sound...
- wruza 2y agoDoes it make sense though? What should happen if a type hints a string and there’s a number in a received json? We gonna segfault?
- 2y ago
- christophilus 2y agoBun is so nice. I’m building a little, zero dependency web application just for fun. - TSX is baked in, so I’m using that as my HTML templating layer. - SQLite is baked in, so I’m using that as my database. - Bun shell replaced the need for something like “concurrently”. - I wired together live reloading plumbing using easily using the file watcher. Its documentation is excellent. It starts up fast. When you do need to install dependencies, that is impressively fast, too. If you haven’t checked it out yet, I’d highly recommend it. 9/10. (The reason for the 9 is that there are still some lingering APIs and behaviors missing from Node which might trip you up.)
- brabel 2y ago> - I wired together live reloading plumbing using easily using the file watcher. Can you expand on that? I was looking for something that lets me reload JS code just yesterday, and apparently the recommended way to do it is to restart the process :D.
- christophilus 2y agoOK. I just took a look at the project, and I was wrong. I'm not using the file watcher anymore because I change my architecture (from an SPA to a traditional server-rendered application). I run bun in `--watch` mode which means the server restarts when I make changes. The live-reload logic is around 100 LOC. To make the client refresh when the server restarts, I wrote a little middleware function that runs in dev mode, but not production. It inspects the response, and if it detects a `text/html` response, it replaces `</body>` with `<script>...</script></body>` where the script performs a long-poll fetch to an endpoint that the middleware also injects (listening on `/{someuuid}`). If the long-poll connection drops, the client begins a reconnect loop, and once it reconnects, it reloads. That's the gist of it, anyway. It's not as complex as it sounds, and it works quite well for my needs.
- FuckButtons 2y agoIn general I’ve found watchexec super useful since you can wire together arbitrary build steps, which once they get to unwieldy or ossified go into a just file.
- sureIy 2y agoI really honestly wish node and npm would stop development already because they’ve been a huge drag on the entire ecosystem. GitHub could “buy” Bun, fire the entire npm team outside the registry and directly compete with Node.
- taspeotis 2y agoThe string problem goes back quite a while https://news.ycombinator.com/item?id=8704318 https://news.ycombinator.com/item?id=8704318
- punkbit 2y agoBun is fantastic! It saves me so much time. Thank you!
- sharas- 2y agobun is coppying what Deno already did, and then hipes it, without mentioning Deno.
- tracker1 2y agoNot quite... I'm a fan of Deno myself and have been using it as a shell scripting language for a lot of things. That said, Bun has had some different points of focus, some better imo, some worse. One thing Bun did, that I wish Deno had was a built in API for Sqlite databases. Deno has had the library in the runtime for a while, but has resisted exposing it directly. While I get the reasons, it would help with security integration over the other options IMO. I think integrating SQLite (libsql) and even Tuirso/AstroDB (libsql-server) support in the box could really be a boost to app development productivity myself. While the latter would be more difficult in self-hosted scenarios.
- dangcontinent 2y ago[flagged]
- gg2222 2y agoHow stable is Bun these days? Does it crash often? Is it ready for production app server use yet?
- lazyc97 2y agoHave only used it for small projects but it doesn't just "crash" out of no where. There are only 2 issues I encountered: - Packages using Node functions Bun hasn't implemented yet. Google generative ai sdk streaming mode doesn't work, the rest of packages work fine for me though. - Bun won't shutdown at the end of the script if there are async functions running in the background. I have to close the DB at the end of every script instead of just using pre-exit hook.
- deleted 2y ago[deleted]