Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Recursion vs While async v2
(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(10000).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 < 5000) { await getItems(nextPage + 1) } }
While
let items = [] const getItems = async (nextPage = 0) => { const _res = await getData() items.push(_res) let _nextPage = nextPage while (_nextPage < 5000) { 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
55135072.0 Ops/sec
While
52324452.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark JSON and explain what's being tested, compared, and considered. **Benchmark Overview** The provided JSON represents two individual test cases: Recursion and While, which are variations of fetching data asynchronously using JavaScript. The tests aim to compare the performance of these two approaches on a large dataset. **Script Preparation Code** The script preparation code defines a function `getData()` that returns an array of 10,000 elements with indices ranging from 0 to 9,999. This function is used as a placeholder for retrieving data asynchronously. ```javascript const response = Array(10000).fill(null).map((item, index) => `foo ${index}`) ``` **Benchmark Preparation Code** The benchmark preparation code is the same for both test cases: ```javascript const getData = async () => { await new Promise(resolve => setTimeout(() => resolve(), 1000)) return response } ``` This function simulates an asynchronous data retrieval operation by waiting for 1 second before returning the `response` array. **Test Cases** There are two test cases: ### Recursion ```javascript const getItems = async (nextPage = 0) => { const _res = await getData() items.push(_res) if (nextPage < 5000) { await getItems(nextPage + 1) } } ``` This test case uses recursion to fetch the data. The function calls itself recursively until `nextPage` reaches 5000, incrementing the index by 1 each time. ### While ```javascript const getItems = async (nextPage = 0) => { const _res = await getData() items.push(_res) let _nextPage = nextPage while (_nextPage < 5000) { const _res = await getData() items.push(_res) _nextPage += 1 } } ``` This test case uses a `while` loop to fetch the data. The loop continues until `_nextPage` reaches 5000, incrementing the index by 1 each time. **Options Compared** The two test cases compare: * **Recursion vs While**: Two different approaches for fetching data asynchronously. * **Async vs Not async**: Both tests use `async/await`, but the `While` test case uses a synchronous `while` loop to wait for the next iteration, whereas the `Recursion` test case uses asynchronous recursion. **Pros and Cons** Here are some pros and cons of each approach: ### Recursion Pros: * Easier to implement and understand, especially for smaller datasets. * Can be more elegant and concise in code. Cons: * May lead to stack overflow errors for large datasets due to recursive function calls. * Can be slower than iterative approaches due to the overhead of recursive calls. ### While Pros: * More memory-efficient than recursion, as it doesn't create new stack frames. * Can be faster than recursive approaches for large datasets. Cons: * More complex and harder to understand, especially for those unfamiliar with loops. * May lead to performance issues if not optimized properly (e.g., using `setTimeout()` or `setInterval()`). **Library Used** The test cases use a placeholder library called `getData()`, which is not a real JavaScript library. This function simulates an asynchronous data retrieval operation and is used as a starting point for both tests. **Special JS Features or Syntax** There are no special features or syntax mentioned in the benchmark JSON, but it's worth noting that JavaScript has several other features like `Promise.all()`, `async/await` with `try-catch`, and `Promise.resolve()` which can be used to simplify asynchronous code.
Related benchmarks:
Promise vs async vs callbacks
Promise vs Async Await(2)
Async vs Callback
Recursion vs While async
Comments
Confirm delete:
Do you really want to delete benchmark?