Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice version 2
(version: 0)
Comparing performance of:
BySlice vs ByReduce
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const chunkByLoop = (array,size)=>{ const chunked = []; for(let i=0;i<array.length;i++){ const chunk = chunked[chunked.length-1]; chunk?.length < size ? chunk.push(array[i]) : chunked.push([array[i]]) } return chunked; } const chunkByReduce = (array,size)=>{ const result = array.reduce(((acc, item)=>{ const chunk = acc[acc.length-1]; chunk?.length < size ? chunk.push(item) : acc.push([item]) return acc; }),[]); return result; } const chunkBySlice = (array,size)=>{ const chunked = []; if (size === 0) return chunked; const slicesNumber = array.length%size ? Math.ceil(array.length/size) : array.length/size; for(let i=0;i<slicesNumber;i++){ chunked.push(array.slice(i*size,i*size+size)) } return chunked; } var checkResult = (expected,actual)=>{ return Array.isArray(expected) && Array.isArray(actual) && expected.length === actual.length && expected.every((chunk,index)=> Array.isArray(chunk) && chunk.every(item => actual[index].includes(item))) } var funMap = { "ByLoop":chunkByLoop, "ByReduce":chunkByReduce, "BySlice":chunkBySlice };
Tests:
BySlice
const chunk = funMap["BySlice"]; const arrEmpty = chunk([],1); console.log("case 1",checkResult(arrEmpty,[])); const simpleCase = chunk([1,2,3,4],2); console.log("case 2",checkResult(simpleCase,[[1,2],[3,4]])); const advancedCase = chunk([1,2,3,4],3); console.log("case 3",checkResult(advancedCase,[[1,2,3],[4]]));
ByReduce
const chunk = funMap["ByReduce"]; const arrEmpty = chunk([],1); console.log("case 1",checkResult(arrEmpty,[])); const simpleCase = chunk([1,2,3,4],2); console.log("case 2",checkResult(simpleCase,[[1,2],[3,4]])); const advancedCase = chunk([1,2,3,4],3); console.log("case 3",checkResult(advancedCase,[[1,2,3],[4]]));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
BySlice
ByReduce
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/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
BySlice
5904.6 Ops/sec
ByReduce
7581.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and test cases to explain what's being tested, compared, and their pros/cons. **Benchmark Definition:** The benchmark defines three functions for chunking an array of items into smaller groups: 1. `chunkByLoop`: uses a loop to iterate through the array and push/push elements into chunks. 2. `chunkByReduce`: uses the `reduce` method to accumulate chunks. 3. `chunkBySlice`: uses the `slice` method to divide the array into chunks. The benchmark also defines a comparison function (`checkResult`) that verifies if two arrays have the same structure and contents. **Test Cases:** There are two test cases: 1. **"BySlice"`: * Verifies that an empty chunk is returned when the input array is empty. * Checks if chunks are correctly split for simple cases (e.g., `[1, 2, 3, 4]` with a chunk size of `2`). * Tests advanced cases (e.g., `[1, 2, 3, 4]` with a chunk size of `3`). 2. **"ByReduce"`: * Similar to the "BySlice" test case, but uses the `reduce` method instead. **Comparison:** The benchmark compares the performance of these three chunking methods: 1. `chunkByLoop`: uses a loop, which can be slower than other approaches. 2. `chunkByReduce`: uses the `reduce` method, which is often faster and more efficient. 3. `chunkBySlice`: uses the `slice` method, which can be convenient but might not be as fast as `chunkByReduce`. **Pros and Cons:** * **chunkByLoop**: Pros: simple to implement; Cons: can be slower due to loop overhead. * **chunkByReduce**: Pros: often faster and more efficient; Cons: requires understanding of the `reduce` method. * **chunkBySlice**: Pros: convenient and easy to use; Cons: might not be as fast as `chunkByReduce`. **Library:** The benchmark uses a custom `funMap` object that maps string keys to function values. The functions (`chunkByLoop`, `chunkByReduce`, `chunkBySlice`) are not part of any popular JavaScript library. **Special JS Features/Syntax:** * None mentioned in the provided code. **Alternatives:** Other chunking methods could be tested, such as: 1. Using a library like Lodash's `chunk` function. 2. Implementing a custom chunking algorithm using recursion or iterative approaches. 3. Testing different data structures, like arrays of objects or matrices. Keep in mind that the specific alternatives will depend on the project requirements and goals.
Related benchmarks:
Chunking array
Lodash Chunk vs Native Reduce v2
Lodash Chunk vs Native Reduce v3
Lodash Chunk vs Native Chunk
Comments
Confirm delete:
Do you really want to delete benchmark?