Bun with Sveltekit (Benchmarks / Docker)

Bun 1.0 was released . It came with a lot of hype , So i was curious to try BUN with Sveltekit. If the claims of bun was true , The…

Bun with Sveltekit (Benchmarks / Docker)

Bun 1.0 was released . It came with a lot of hype , So i was curious to try BUN with Sveltekit. If the claims of bun was true , The performance benefits which we will achive is going to be on another level when we compare it with node.


Setting up Svletekit

you can get bun from bun.sh
# Using bunx instead of using npx or pnpm dlx 
bunx create-svelte

You can now answer few questions and get your basic sveltekit app.

#Move to your app directory 
cd your_app/ 
# Using bun pacakge manager 
bun i

Thats it you have a Svletkit app Configured let try it out now

#start the dev server 
bun dev

Now your app will be live on localhost:5173.


Benchmarks

Bun comes with a lot of promises on performance. I will use bun-adapter and node-adapter for benchmarking sveltekit.

#setup bun adapter 
bun i -D svelte-adapter-bun

Now you can update your adapter in your svelte.config.js

import adapter from 'svelte-adapter-bun'; 
import { vitePreprocess } from '@sveltejs/kit/vite'; 
 
/** @type {import('@sveltejs/kit').Config} */ 
const config = { 
 preprocess: vitePreprocess(), 
 kit: { 
  adapter: adapter() 
 } 
}; 
 
export default config;
#build you app 
bun run build 
 
cd build/ 
 
#server your app 
bun start

This will run your app on localhost:3000 , But note that you are using the built in Bun HTTP Server .

Lets use oha for testing our Servers , we will try to hit a simple SSR example.

The difference is pretty significant over 50%, But these result are not the actual performance you would expect from a actual server because there are lot of other factors which comes into play when you go into real world scenario, But still Bun seems to have an upper hand.

Lets also do some load testing with K6.

You can view the results of NodeJs and Bun for further digging up.

Dockerize our Sveltekit app

Make sure you have docker installed .

create a Dockerfile in your root directory

# Step 1: Build the application 
FROM oven/bun AS builder 
 
# Set the working directory in the container 
WORKDIR /app 
 
# Copy all the application files to the container 
COPY . . 
 
# Run your build process 
RUN bun i 
RUN bun run build 
 
# Step 2: Create a smaller image for running the application 
FROM oven/bun 
 
# Copy only the necessary files from the builder image to the final image 
COPY --from=builder /app/build . 
 
# Expose the port the application will run on 
EXPOSE 3000 
 
#Start the BUN server 
CMD ["bun", "run", "start"]

Create a .dockerignore file

node_modules/ 
.svelte-kit/ 
build/

Then for building you app create a docker-compose.yaml

version: "1" 
services: 
  external: 
    container_name: your_container 
    # The name of the image that will be created when building this container 
    image: yourid/image 
    build: 
      context: . 
      dockerfile: Dockerfile 
    ports: 
      - 8080:3000

Build you docker app.

docker-compose build

Then you can preview it by running

docker-compose up

you can see the output of the docker image in localhost:8080.

Here is my Docker image.

To just try out my image run these commands

#pull the docker image 
docker pull anasmohammed361/svelte-bun-blog 
 
#run the image 
docker run -p 8080:3000 anasmohammed361/svelte-bun-blog

You can then deploy the docker image to something like Cloud Run or you favourite Hosting Platform which supports Docker images.

Conclusion

Bun is new to the Production environment. Though it might not be as stable as node js , It might be a replacement for node.js one day. It has lot of features baked into it for the developers to play with , So why not give it a try ? …..