Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Recursion vs While async
(version: 0)
Comparing performance of:
Recursion vs While
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const response = Array(1000).fill(null).map((item, index) => `foo ${index}`) const getData = async () => { await new Promise(resolve => setTimeout(() => resolve(), 1000)) return response }
Tests:
Recursion
let items = [] const getItems = async (nextPage = 0) => { const _res = await getData() items.push(_res) if (nextPage < 5) { await getItems(nextPage + 1) } }
While
let items = [] const getItems = async (nextPage = 0) => { const _res = await getData() items.push(_res) let _nextPage = nextPage while (_nextPage < 5) { const _res = await getData() items.push(_res) _nextPage += 1 } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Recursion
While
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Recursion
56513760.0 Ops/sec
While
70067352.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is tested, options compared, pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark defines two test cases: 1. "Recursion" - This test case uses recursion to fetch data from a remote API (`getData()` function). 2. "While" - This test case uses a while loop to fetch data from the same remote API (`getData()` function). **Script Preparation Code** Both test cases use the following script preparation code: ```javascript const response = Array(1000).fill(null).map((item, index) => `foo ${index}`) const getData = async () => { await new Promise(resolve => setTimeout(() => resolve(), 1000)) return response } ``` This script prepares an array of 1000 items and defines a function `getData()` that simulates a remote API call with a 1-second delay. **Html Preparation Code** Neither test case has an HTML preparation code, which means the benchmark only runs in Node.js and does not involve any browser rendering or DOM manipulation. **Individual Test Cases** Each test case uses the following code: ```javascript let items = [] const getItems = async (nextPage = 0) => { const _res = await getData() items.push(_res) if (nextPage < 5) { await getItems(nextPage + 1) } } ``` For the "Recursion" test case, the `getItems` function uses recursion to fetch data from the `getData()` function. For the "While" test case, the `getItems` function uses a while loop to fetch data from the same `getData()` function. **Options Compared** The two test cases compare: 1. Recursion vs While loop approach 2. Use of async/await vs traditional callback-based approach **Pros and Cons of Each Approach** Recursion: Pros: * Easier to understand and implement for small datasets * Can be more elegant and concise code Cons: * Performance overhead due to function call stack usage * May cause stack overflow errors for large datasets * Can lead to inefficient memory allocation While loop: Pros: * More efficient performance, as it avoids the overhead of function calls * Can handle larger datasets without causing stack overflow errors * Less prone to memory allocation issues Cons: * More complex and harder to understand for beginners * May require more code and management of loop variables **Other Considerations** * Both test cases use the same remote API (`getData()` function), which ensures a fair comparison between the two approaches. * The `while` loop test case uses a while loop with a fixed number of iterations (5), whereas the recursion test case has no explicit limit on the number of recursive calls. This means that the while loop test case is more predictable and less prone to performance issues. **Other Alternatives** If you're looking for alternative approaches, here are some options: 1. Iterative approach: Use a traditional loop (e.g., `for` or `while`) instead of recursion or async/await. 2. Memoization: Cache the results of expensive function calls to avoid redundant computations. 3. Parallel processing: Use web workers or other parallel processing techniques to speed up performance-critical sections of code. Keep in mind that the choice of approach depends on your specific use case, performance requirements, and coding style preferences.
Related benchmarks:
Promise vs async vs callbacks
Promise vs Async Await(2)
Async vs Callback
Recursion vs While async v2
Comments
Confirm delete:
Do you really want to delete benchmark?