← Back to all posts

Benchmarking JavaScript Runtimes: How bun.sh Stands Up Against Node.js in Recursive Loops

published - |written by Joe Peterson
Side-by-side comparison of JavaScript runtimes showcasing bun.sh's speed and efficiency over Node.js in recursive loop processing, with a dynamic backdrop of performance metrics and code, reflecting cutting-edge web development technologies.

Side-by-side comparison of JavaScript runtimes showcasing bun.sh's speed and efficiency over Node.js in recursive loop processing, with a dynamic backdrop of performance metrics and code, reflecting cutting-edge web development technologies.

Introduction

In the fast-evolving landscape of JavaScript runtimes, performance metrics are a hot topic. The arrival of bun.sh has opened new discussions on runtime efficiency, particularly for recursive loops—a challenging yet common scenario in programming. In this post, we put bun.sh and Node.js head-to-head to see how they fare in managing complex recursive operations.

Methodology: Crafting the Comparison

To gauge the performance of bun.sh against Node.js, we crafted a series of tests that mimic the behavior of recursive for loops. Using TypeScript, we implemented identical loop logic in both environments, tracking execution times and memory usage across varying depths of recursion.

The Approach to Recursive Loops

Recursive functions can quickly become resource-intensive, making them an excellent benchmark for runtime efficiency. Our tests involved implementing a recursive algorithm that calculated the sum of a series of numbers, increasing the recursion depth with each iteration.

node.js vs bun.sh recursive loop performance graph

Each test was run multiple times to ensure consistency, and the results were carefully logged for analysis. The chosen recursive function was designed to push the runtimes to their limits, providing a clear picture of their performance under pressure.

Analysis: Execution Time and Memory Usage

Initial Observations (Depths 1-3)

From the onset, both bun.sh and Node.js exhibited similar execution times, but a trend emerged showing bun.sh's superior memory efficiency. At depth 1, Node.js had a slightly lower mean execution time at 0.0142 ms compared to bun.sh’s 0.0161 ms, but it used considerably more memory (mean of 5.8652 MB for Node.js against 1.4089 MB for bun.sh). As we increased the depth, bun.sh began to outperform Node.js in both execution time and memory usage, which became more noticeable at depth 3, where bun.sh’s execution time mean was 0.1766 ms with a memory usage mean of 1.7949 MB, whereas Node.js posted a mean execution time of 0.2448 ms with a memory usage mean of 6.2239 MB.

Mid-Level Testing (Depths 4-6)

Moving into the mid-level depths, the gap widened. At depth 4, bun.sh’s mean execution time was 1.2596 ms, contrasting with Node.js’s 2.0537 ms. Memory usage remained more efficient for bun.sh, averaging 2.0944 MB compared to Node.js's 6.3826 MB. By depth 6, the difference in execution time was more pronounced; bun.sh averaged at 122.6465 ms, almost 35% faster than Node.js's 187.1683 ms. Regarding memory, bun.sh used an average of 2.5624 MB, maintaining its lead over Node.js, which averaged 6.9020 MB.

Deep Recursion (Depths 7-9)

As we ventured into the deeper levels of recursion, bun.sh's performance benefits became even more evident. At depth 7, bun.sh completed tasks with a mean execution time of 1232.1892 ms, whereas Node.js lagged behind with a mean of 1899.1045 ms. The memory usage for bun.sh was also lower, with a mean of 3.3887 MB compared to Node.js’s 7.8682 MB. This trend of bun.sh outpacing Node.js continued through depths 8 and 9, where bun.sh’s mean execution times were 15204.0159 ms and 163026.9187 ms respectively, against Node.js’s 18316.1047 ms and 183032.4489 ms. Memory usage figures at these depths further underscored bun.sh's efficiency, with its memory usage peaking at 12.7907 MB at depth 9, while Node.js peaked at 22.5308 MB.

Compiled Results

Depths (grouped)

1-3

4-6

7-9

Fastest Environment

bun.sh

bun.sh

bun.sh

% Faster

24.04%

34.31%

13.32%

% Less Memory Used

73.65%

64.35%

51.13%

bun.sh Mean Time (ms)

0.075

45.39

59154.37

node.js Mean Time (ms)

0.1

69.12

68249.22

bun.sh Mean Memory (MB)

1.59

2.35

5.54

node.js Mean Memory (MB)

6.04

6.61

11.34

In this table, the data is grouped into three depth ranges, with averages calculated for each group. The fastest environment for each group is determined based on the mean execution time, and the percentages `faster` and `less memory used` are calculated accordingly.

The data from these tests paint a clear picture: bun.sh not only maintains a lower memory footprint but also accelerates execution time as the complexity of recursion increases. This could have significant implications for developers and the tech industry at large, influencing decisions in choosing the most suitable runtime for server-side JavaScript execution.

You can view the detailed reports on the bun.sh analysis and node.js analysis on our GitHub repository.

Key Findings: When Performance Is Paramount

Throughout the depths tested, bun.sh has consistently showcased its prowess. Notably, in the early stages (Depths 1-3), it began to edge out Node.js by being 24.04% faster and using 73.65% less memory. This initial performance set the tone for the subsequent tests.

As we journeyed into the mid-range depths (4-6), bun.sh's performance lead solidified. It outpaced Node.js by a significant 34.31%, all the while maintaining a substantial lead in memory efficiency with 64.35% less memory usage. For developers, this performance advantage represents a substantial improvement, particularly in applications where execution speed is critical.

In the realm of deep recursion (Depths 7-9), bun.sh continued to surpass Node.js, completing tasks 13.32% faster and utilizing 51.13% less memory. These figures are not merely numbers; they translate into real-world efficiencies and savings in both time and resources.

The mean execution times and memory usage across all depths speak volumes about bun.sh's capabilities. Where Node.js presented a mean time of 68,249.2194 ms in the most intense depth bracket, bun.sh averaged at 59,154.3746 ms. That translates to nearly 9 seconds faster response in average. In terms of memory, while Node.js consumed a mean of 11.3358 MB, bun.sh required only 55.533 MB. A whopping 48.8% reduction.

Conclusion: A Decisive Victory for bun.sh in Performance

The data doesn't just hint at a trend; it shouts a clear message: under the strain of our testing, bun.sh has demonstrated a decisive performance advantage over Node.js. For the modern developer, where efficiency and speed are not just desired but required, bun.sh offers a compelling alternative that could reshape the landscape of server-side JavaScript execution.

These findings suggest that bun.sh could be particularly advantageous for data-intensive operations, complex recursive tasks, and real-time processing applications. Its ability to handle demanding computational tasks with less memory and greater speed could make it the preferred choice for developers looking to maximize the performance of their JavaScript applications.

We encourage you to witness these results firsthand. Dive into the detailed reports, replicate our benchmarks, and see if bun.sh can elevate your applications to the next level of performance:

Our tests are a starting point. We invite the community to expand upon this work, challenge our findings, and contribute to a collective understanding. Will bun.sh continue to outperform as it matures? That's for the community to test, verify, and decide. But for now, in our tests, bun.sh stands as the unrivaled leader in recursive loop execution performance.

Recommended Posts