Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Batching
(version: 5)
Batching strategy with yield benchmark
Comparing performance of:
For loop vs Yield
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function chunkArray(arr, chunkSize) { const result = []; for (let i = 0; i < arr.length; i += chunkSize) { const chunk = arr.slice(i, i + chunkSize); result.push(chunk); } return result; }
Tests:
For loop
function task(data) { return new Promise((resolve, reject) => { const results = data.reduce((acc, curr) => acc + curr, 0) resolve(results) }) } const datasets = [...Array(5_000_000).keys()] for (chunk of chunkArray(datasets, 10000)) { task(chunk).then( (res) => { // } ) }
Yield
function* task(data) { return new Promise((resolve, reject) => { const results = data.reduce((acc, curr) => acc + curr, 0) resolve(results) }) } function* process(data) { for (chunk of chunkArray(data, 10000)) { yield task(chunk) } } const datasets = [...Array(5_000_000).keys()] for (data of process(datasets)) { const resp = data.next().value resp.then((res) => { // }) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For loop
Yield
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For loop
2.1 Ops/sec
Yield
2.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and some pros/cons of each approach. **Batching Strategy with Yield Benchmark** The provided JSON represents a JavaScript microbenchmark that tests the performance of two different batching strategies: using for loops and yield statements. The script preparation code defines a function `chunkArray` that takes an array and a chunk size as input and returns an array of chunks. **Test Cases: For Loop and Yield** There are two individual test cases: 1. **For Loop**: This test case uses a traditional for loop to process the chunks of data. The script preparation code defines a function `task` that calculates the sum of an array using the reduce method. The test script loops through the chunked data using the `chunkArray` function and calls the `task` function for each chunk. 2. **Yield**: This test case uses a generator function (`process`) with yield statements to process the chunks of data. The script preparation code defines two functions: `task`, which calculates the sum of an array, and `process`, which loops through the chunked data using the `chunkArray` function and yields the results. **Comparison** The benchmark compares the performance of these two batching strategies: * **For Loop**: This approach uses a traditional loop to process the chunks of data. It's a straightforward and easy-to-understand approach, but it may not be as efficient as other methods. * **Yield**: This approach uses generator functions with yield statements to process the chunks of data. Generator functions can provide better performance than traditional loops because they allow for lazy evaluation and don't create new scopes. **Pros and Cons** Here are some pros and cons of each approach: * **For Loop**: + Pros: Easy to understand, straightforward implementation. + Cons: May not be as efficient due to the creation of new scopes and variables. * **Yield**: + Pros: Can provide better performance due to lazy evaluation and no scope creation. Allows for easier management of side effects. + Cons: Requires understanding of generator functions, which may be unfamiliar to some developers. **Library Usage** There is no explicit library usage in these test cases. However, the `chunkArray` function uses the spread operator (`...`) to create a new array from an existing one, which is a JavaScript feature introduced in ES6. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in these test cases. **Other Alternatives** Some alternative batching strategies that could be tested in this benchmark include: * **Async/await**: Using async functions and await statements to process the chunks of data. * **Pipelining**: Processing the chunks of data in a pipeline fashion, where each chunk is processed immediately after the previous one. * **Parallel processing**: Processing multiple chunks of data concurrently using parallel processing techniques. These alternative approaches could provide interesting insights into the performance differences between different batching strategies.
Related benchmarks:
Chunking array
Split large array into multiple chunked array
Lodash Chunk vs Native Reduce v2
slice version 2
Comments
Confirm delete:
Do you really want to delete benchmark?