DEV Community

Kaushik Mitra
Kaushik Mitra

Posted on

Use bunx for running npm packages with lightning fast speed.

Introduction

bun is the latest and greatest runtime for Javascript which ships with many features like built in Typescript support and testing support and etc. etc.. bunx, alias for bun x, is a package manger for bun which can be used for auto installing and running npm packages. We can you bunx just similarly like we use npx or yarn dlx.

According to this twitter post, bunx is 100x faster than npx, which means we can use bunx and run our npm cli with lightning fast speed.

Today we will discuss an use case of running a npm package with bunx and compare the result with running it with npx.

You can view the package from here, it is a simple package that I wrote which generates a random quote of Phil Dunphy from Mordern Family. You can also view a post that I have written with my learnings and take aways from creating this package.

Use case for the package

As the package phil-s-osophy just spits out basic string output we can pipe that to any program, like I have used to pipe the value in cowsay and added the whole thing at the bottom of my zshrc file to show text at every shell creation.

bunx phil-s-osophy | bunx cowsay
Enter fullscreen mode Exit fullscreen mode

Comparing with npx

To compare my changes with the npx, I have created a function which will run the shell few times and time it.

timezsh() {
    for i in $(seq 1 10); do /usr/bin/time $SHELL -i -c exit; done
}
Enter fullscreen mode Exit fullscreen mode

So after running both my program with bunx and npx I timed it to aroud 0.3 to 0.4s when running with bunx but when running with npx the program took around 2s to ran.

Note: there are many more things happening in my zshrc file, but I am ignoring this as they are running in bot scenarios.

Left running - bunx phil-s-osophy | bunx cowsay
Right running - npx phil-s-osophy | npx cowsay

comapare bunx v npx - screengrab

Conclusion

So, in conclusion we have seen how bunx is so much faster than npx and how we can use this lightning fast speed to install and use packages for that great developer experience.

For more information about the bun, you can visit bun.sh.

Top comments (0)