Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Recursion vs Iteration
(version: 2)
Comparing performance of:
recursion vs while loop vs for loop
Created:
8 years ago
by:
Registered User
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) { i = i || 0; 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:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
recursion
19307096.0 Ops/sec
while loop
101363376.0 Ops/sec
for loop
100592600.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Definition JSON** The `Script Preparation Code` section defines three functions: `sum_recurse`, `sum_while`, and `sum_for`. These functions are designed to calculate the sum of an array of numbers using different approaches: 1. **Recursion**: The `sum_recurse` function uses a recursive approach, where each element is added to the sum of the remaining elements in the array. 2. **While Loop**: The `sum_while` function uses a while loop to iterate through the array, adding each element to the sum. 3. **For Loop**: The `sum_for` function uses a for loop to iterate through the array, adding each element to the sum. **Options Compared** The benchmark compares the execution time of these three functions on the same input data (`nums`). This allows users to compare the performance of different approaches in calculating the sum of an array. **Pros and Cons of Different Approaches** 1. **Recursion**: Recursion can be a elegant and concise way to solve problems, but it can also lead to stack overflow errors if the recursion is too deep. In this case, the recursion may not terminate quickly enough due to the large input size. 2. **While Loop**: A while loop is a straightforward approach that directly iterates through the array, making it potentially faster than recursive approaches for large inputs. 3. **For Loop**: A for loop is another common approach to iterate through an array, which can be more efficient than using a while loop. **Library and Syntax Considerations** There is no explicitly mentioned library in this benchmark, but the use of `var` and `function` keywords suggests that it's running in a JavaScript environment without strict mode enabled (since there are no explicit type declarations or strict mode syntax). The syntax used here is relatively old-school for modern JavaScript, which may be due to the fact that the benchmark is designed to test basic execution performance rather than more advanced features. **Special JS Features and Syntax** This benchmark does not use any special JavaScript features or syntax. It appears to be a basic benchmark to compare the performance of different iteration approaches in JavaScript. **Other Alternatives** If you want to explore alternative approaches, you could consider using: 1. **Array.prototype.reduce()**: A more modern approach that uses a single method call to reduce the array to a single value. 2. **Generator Functions**: An alternative to recursion that uses generators to produce values over time. 3. **Closures**: You could use closures to implement your own iteration logic. These alternatives would require additional modifications to the benchmark script and may introduce new considerations, such as code readability and maintainability.
Related benchmarks:
recursion vs iteration bench
Recursion vs Iteration (with tail recursion)
Recursion vs Iteration (with tail recursion)2
Recursion vs Iteration v2
Comments
Confirm delete:
Do you really want to delete benchmark?