Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Recursion vs Iteration v2
(version: 0)
Comparing performance of:
recursion vs while loop vs for loop
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; function sum_recurse(arr, i) { if (i > arr.length) return 0; else return arr[i] + sum_recurse(arr, i + 1); } function sum_while(arr) { var total = 0, i = 0, len = arr.length; while (i < len) { total += arr[i++]; } return total; } function sum_for(arr) { var total = 0, len = arr.length; for (var i = 0; i < len; i++) { total += arr[i]; } return total; }
Tests:
recursion
sum_recurse(nums)
while loop
sum_while(nums)
for loop
sum_for(nums)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
recursion
while loop
for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand what's being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark is comparing three different approaches to calculate the sum of an array: recursion, while loop, and for loop. The benchmark measures which approach performs better in terms of execution speed on a specific device (Desktop, Windows). **Options Compared** 1. **Recursion**: This approach uses function calls to iterate through the array. It's a simple and intuitive way to calculate the sum, but it can be slower due to the overhead of function calls. 2. **While Loop**: This approach uses a while loop to iterate through the array. It's similar to recursion, but avoids the function call overhead by using a explicit loop. 3. **For Loop**: This approach uses a for loop to iterate through the array. It's often considered the most efficient way to iterate through an array. **Pros and Cons** * **Recursion**: + Pros: Easy to understand and implement, doesn't require manual indexing. + Cons: Can be slower due to function call overhead, may cause stack overflow for large arrays. * **While Loop**: + Pros: Avoids function call overhead, easy to implement, flexible. + Cons: Requires manual indexing, can be harder to understand than recursion or for loop. * **For Loop**: + Pros: Fastest and most efficient way to iterate through an array, easy to understand. + Cons: Limited flexibility compared to while loop. **Library Usage** None of the benchmark code uses any external libraries. However, it's worth noting that some modern JavaScript engines may use just-in-time (JIT) compilation or other optimization techniques that could impact the results. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. The code is standard vanilla JavaScript. **Other Considerations** The benchmark only measures execution speed, not memory usage or other performance metrics. It's possible that other factors like garbage collection or caching could affect the results on certain devices or browsers. If you're interested in exploring alternative benchmarks, some popular alternatives include: * **jsperf**: A classic benchmarking tool for JavaScript that allows users to create and run custom benchmarks. * **benchmark.js**: A modern benchmarking library that provides a simple and flexible way to measure performance metrics like execution speed and memory usage. * **Google's Closure Benchmark**: A comprehensive benchmarking suite that tests various JavaScript algorithms and data structures.
Related benchmarks:
Recursion vs Iteration
recursion vs iteration bench
Recursion vs Iteration (with tail recursion)
Recursion vs Iteration (with tail recursion)2
Comments
Confirm delete:
Do you really want to delete benchmark?