Skip to main content
Bun is designed for speed. Hot paths are extensively profiled and benchmarked. The source code for all of Bun’s public benchmarks is in the /bench directory of the Bun repo.

Measuring time

To measure time precisely, Bun offers two runtime APIs:
  1. The Web-standard performance.now() function
  2. Bun.nanoseconds(), which is like performance.now() except it returns the time since the application started in nanoseconds. Use performance.timeOrigin to convert this to a Unix timestamp.

Benchmarking tools

  • For microbenchmarks, we recommend mitata.
  • For load testing, you must use an HTTP benchmarking tool that is at least as fast as Bun.serve(), or your results will be skewed. Some popular Node.js-based benchmarking tools like autocannon are not fast enough. We recommend one of the following:
  • For benchmarking scripts or CLI commands, we recommend hyperfine.

Measuring memory usage

Bun has two heaps: one for the JavaScript runtime, and one for everything else.

JavaScript heap stats

The bun:jsc module exposes a few functions for measuring memory usage:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35b
JavaScript is a garbage-collected language, not reference counted. It’s normal and correct for objects to not be freed immediately in all cases, though it’s not normal for objects to never be freed. To force garbage collection to run manually:
Heap snapshots show which objects are not being freed. Use Bun.generateHeapSnapshot() to take a heap snapshot, then view it with Safari or WebKit GTK developer tools. To generate a heap snapshot:
To view the snapshot, open the heap.json file in Safari’s Developer Tools (or WebKit GTK):
  1. Open the Developer Tools
  2. Click “Timeline”
  3. Click “JavaScript Allocations” in the menu on the left. It might not be visible until you click the pencil icon to show all the timelines
  4. Click “Import” and select your heap snapshot JSON
Importing a heap snapshot
Once imported, you should see something like this:
Viewing heap snapshot in Safari
The web debugger timeline also tracks the memory usage of the running debug session.

Native heap stats

Bun uses mimalloc for the other heap. To print a summary of non-JavaScript memory usage, call Bun.unsafe.mimallocDump().

CPU profiling

Profile JavaScript execution to identify performance bottlenecks with the --cpu-prof flag.
terminal
--cpu-prof writes a .cpuprofile file you can open in Chrome DevTools (Performance tab → Load profile) or VS Code’s CPU profiler.

Markdown output

Use --cpu-prof-md to generate a markdown CPU profile, which is grep-friendly and designed for LLM analysis:
terminal
Combine --cpu-prof and --cpu-prof-md to generate both formats at once:
terminal
You can also pass the flag through the BUN_OPTIONS environment variable:
terminal

Options

terminal
FlagDescription
--cpu-profGenerate a .cpuprofile JSON file (Chrome DevTools format)
--cpu-prof-mdGenerate a markdown CPU profile (grep/LLM-friendly)
--cpu-prof-name <filename>Set output filename
--cpu-prof-dir <dir>Set output directory

Heap profiling

Generate heap snapshots on exit to analyze memory usage and find memory leaks.
terminal
--heap-prof writes a V8 .heapsnapshot file you can load in Chrome DevTools (Memory tab → Load).

Markdown output

Use --heap-prof-md to generate a markdown heap profile for CLI analysis:
terminal
If both --heap-prof and --heap-prof-md are specified, the markdown format is used.

Options

terminal
FlagDescription
--heap-profGenerate a V8 .heapsnapshot file on exit
--heap-prof-mdGenerate a markdown heap profile on exit
--heap-prof-name <filename>Set output filename
--heap-prof-dir <dir>Set output directory