Bun first steps…

Kristan 'Krispy' Uccello
3 min readJul 17, 2022

Bun is the new hotness apparently. Why?

According to the developer headlines its faster in execution (including ffi) and has batteries included (no more web pack — yay). So, after reading a few of the developer tweets on ffi I became interested. The following is just a dump of how I setup a quick playground and tested the bun:ffi (not thoroughly but enough to satisfy my curiosity). Note: my dev box is a Mac Studio with M1 Ultra but these steps should work for any 64-arch system.

Requisites:

  1. Install Rust Lang (so you can compile the rust source for our ffi example below)
  2. Install Bun
mkdir bun-blog
bun add typescript
Starting from an empty directory: `bun add typescript` yields a basic dev env

Next I followed the code that was in the tweet listed above (it was a screen shot of rust code):

  1. Create a file called ‘add.rs’ (our rust source code we will compile to a lib)

2. Execute rustc --crate-type cdylib ./add.rs

Once executed you will have produced a dynamic lib called libadd.dylib (depending on your system it may be an .so or .dll )

3. Dump the symbols of the lib (mac approach):

nm -g libadd.dylib

You should see a bunch of text in your terminal. Look for something like the following in the output.

<some-address-hash> T _add

The reason I did this was to confirm that the symbol was actually in the library. Prior attempt had resulted in an error about an invalid arg type:

Earlier attempt that failed and caused me to look up how to dump dynamic library symbols. Turns out I had not compiled my rs file correctly the first time and the symbol for _add was missing from the dylib file.

Next I pretty much followed the code presented in the tweet:

What this code does:

  1. Holds the loaded “add” symbol from the dylib by calling dlopen (provided by bun:ffi module)
  2. Calls the loaded “add” symbol using FFI and logs the results.
Hey! Look at that it works!

Conclusion

Bun was fast! I mean really fast. It installed typescript faster than I was able to observe. I’m used to npm install so this was a very nice improvement. The fact that bun just works with typescript without the need to transpile or use ts-node is great! (one of my favorite things). The FFI interface is fairly clean and understandable — as someone who has not dabbled too much with napi or ffi before this was a very approachable “dip my toes in the water” experience.

I am encouraged by what Bun has to offer. No I will not use it in production yet it is too soon to trust that much. I will however spend more time playing with it and maybe build out some more serious side projects with it. Already I was able to bring up a KeystoneJS app with it (again it was painless and executed perceivably faster)

Go check out Bun at https://bun.sh/

Lastly, if you enjoyed this short article and want me to write more like it in the future — please consider giving me a clap and or drop me a comment or consider following me.

--

--