Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
slice version 2
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
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:
Chrome 124
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
BySlice
5904.6 Ops/sec
ByReduce
7581.2 Ops/sec
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]]));