Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Chunk map vs map only
(version: 2)
Comparing performance of:
Chunk and map 10000 vs Map only vs Chunk and map 1000 vs Chunk and map 100
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var test = Array.from( { length: 1000000, }, () => ({ b: Math.round(Math.random()), }), ); async function addC(item) { await new Promise((resolve) => setTimeout(resolve, 5)); item.c = 9; return item; } function chunk(arr, chunkSize) { const groups = []; let i = 0; while (i < arr.length) { groups.push(arr.slice(i, (i += chunkSize))); } return groups; } async function mapOnly(array, size) { await Promise.all(array.map(async (item) => await addC(item))); return array; } async function chunkAndMap(array, size) { const chunks = chunk(array, size); await Promise.all( chunks.map(async (chunk) => { await Promise.all(chunk.map(async (item) => await addC(item))); }), ); return array; } async function run(fun, arr, chunkSize) { await fun(arr, chunkSize); }
Tests:
Chunk and map 10000
run(chunkAndMap, test, 10000)
Map only
run(mapOnly, test, 1000)
Chunk and map 1000
run(chunkAndMap, test, 1000)
Chunk and map 100
run(chunkAndMap, test, 100)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Chunk and map 10000
Map only
Chunk and map 1000
Chunk and map 100
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
Chunk and map 1000
0.3 Ops/sec
Map only
0.3 Ops/sec
Chunk and map 100
0.2 Ops/sec
Chunk and map 10
0.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark definition JSON and explain what is being tested. **Benchmark Definition** The `Script Preparation Code` section defines three JavaScript functions: 1. `chunk(arr, chunkSize)`: This function takes an array `arr` and a chunk size `chunkSize`, and returns an array of chunks. Each chunk is a subset of the original array, with a length equal to `chunkSize`. 2. `mapOnly(array, size)`: This function takes an array `array` and a size, and performs a series of promises in parallel using `Promise.all`. It iterates over each element in the array, waits for 5ms (using `setTimeout`) using the `addC` function, updates the element with a new property `c`, and returns the original array. 3. `chunkAndMap(array, size)`: This function is similar to `mapOnly`, but it first uses the `chunk` function to split the array into chunks, then iterates over each chunk using `Promise.all`, and performs the promises in parallel for each chunk. The `run` function takes a benchmark definition string as an argument. It calls one of these functions with the specified parameters (test array `test`, chunk size). **Options being compared** The benchmark compares three options: 1. **mapOnly**: This option uses only the `mapOnly` function, without splitting the array into chunks. 2. **chunkAndMap (with 10000, 1000, and 100)**: These variants of this option use the `chunkAndMap` function with different chunk sizes. **Pros and Cons** Here are some pros and cons for each approach: * `mapOnly`: + Pros: - Simple to implement - Does not require array splitting + Cons: - May be slower due to the overhead of iterating over a large array in parallel - Requires the same amount of CPU time per element, regardless of chunk size * `chunkAndMap (with 10000)`: + Pros: - Should be faster than `mapOnly` because it splits the array into chunks and iterates over each chunk separately + Cons: - Requires array splitting, which can lead to memory allocation overhead - Chunk size must be chosen carefully to minimize overhead * `chunkAndMap (with 1000)` and `chunkAndMap (with 100)`: + Pros: - May offer better performance than `mapOnly` because they split the array into smaller chunks, reducing the CPU time per element + Cons: - Chunk size must be chosen carefully to minimize overhead **Other Considerations** * The use of async/await and promises allows for efficient parallel processing and simplifies the code. * The choice of chunk sizes (10000, 1000, and 100) may impact performance, depending on the characteristics of the input data. **Libraries and Special Features** There are no notable libraries or special features used in this benchmark. However, the use of async/await and promises is a standard JavaScript feature that allows for efficient parallel processing. **Alternatives** Some alternative approaches to compare with `mapOnly` and `chunkAndMap` could include: * Using a more optimized array processing library, such as Array.prototype.reduce() or Array.prototype.forEach() * Implementing the chunking logic using a different data structure, such as an index buffer * Using a parallel computing framework, such as worker threads or Web Workers, to execute the CPU-bound operations in parallel
Related benchmarks:
Map vs Array vs Object vs Set add item speed in 50000 iters 2
foreach for vs promise.all for big arrays
2D nested array of numbers deep copy
Multiple chunk
Comments
Confirm delete:
Do you really want to delete benchmark?