Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
chunk recursive VS while VS lodash.chunk round 2
(version: 1)
Comparing performance of:
splice Recurcive vs splice While vs lodash chunk
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var _list = []; for (var i = 0; i < 1000 * 1000; i++) { _list.push(i); } var chunkR = (array, size) => array.length ? [array.splice(0,size), ...chunkR(array, size)]:[] var chunkW = (array, size) => { const R = new Array(Math.ceil(array.length / size)) while (array.length) R.push(array.splice(0, size)) return R } // lodash shit function slice(array, start, end) { let length = array == null ? 0 : array.length if (!length) { return [] } start = start == null ? 0 : start end = end === undefined ? length : end if (start < 0) { start = -start > length ? 0 : (length + start) } end = end > length ? length : end if (end < 0) { end += length } length = start > end ? 0 : ((end - start) >>> 0) start >>>= 0 let index = -1 const result = new Array(length) while (++index < length) { result[index] = array[index + start] } return result } function toInteger(value) { const result = (value) const remainder = result % 1 return remainder ? result - remainder : result } function chunkL(array, size = 1) { size = Math.max(toInteger(size), 0) const length = array == null ? 0 : array.length if (!length || size < 1) { return [] } let index = 0 let resIndex = 0 const result = new Array(Math.ceil(length / size)) while (index < length) { result[resIndex++] = slice(array, index, (index += size)) } return result } var list = [..._list]
Tests:
splice Recurcive
const list = [..._list] const c = chunkR(list, 200) if (c.length != 5000) throw new Error('bad result')
splice While
const list = [..._list] const c = chunkL(list, 200) if (c.length != 5000) throw new Error('bad result')
lodash chunk
const list = [..._list] const c = chunkL(list, 200) if (c.length != 5000) throw new Error('bad result')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
splice Recurcive
splice While
lodash chunk
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's tested in this benchmark. **Benchmark Overview** The benchmark is designed to compare three different approaches for chunking (dividing) an array: recursive using `splice`, while using a loop, and using the `lodash.chunk` function. The benchmark measures the performance of each approach on an array of 5,000 elements. **Approaches Compared** 1. **Recursive using `splice`**: This approach uses the `splice()` method to remove elements from the original array in chunks. The `splice()` method modifies the original array and returns an array of removed elements. 2. **While loop**: This approach uses a while loop to iterate over the array, removing elements in chunks. 3. **Lodash `chunk` function**: This approach uses the `lodash.chunk` function, which is designed specifically for chunking arrays. **Pros and Cons** 1. **Recursive using `splice`**: * Pros: Simple to implement and easy to understand. * Cons: Modifies the original array, can lead to performance issues due to repeated calls to `splice()`. 2. **While loop**: * Pros: Avoids modifying the original array and can be more efficient than recursive approaches. * Cons: Requires manual looping and element indexing, which can be error-prone. 3. **Lodash `chunk` function**: * Pros: Designed for chunking arrays, optimized for performance, and easy to use. * Cons: Requires including the `lodash` library, which may add overhead. **Library Usage** The benchmark uses the `lodash` library for its `chunk` function. The `lodash` library is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and more. **Special JS Features/Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's typically used in everyday development. **Other Alternatives** If you're interested in exploring alternative approaches, here are some options: * Using `Array.prototype.slice()` to create a copy of the array before chunking it. * Utilizing `Map` data structure to efficiently chunk the array without modifying the original array. * Leveraging modern JavaScript features like `for...of` loops and `Promise.all()` to improve performance. Keep in mind that these alternatives may have their own trade-offs and require careful consideration of performance, memory usage, and code readability.
Related benchmarks:
Lodash Chunk vs Native Reduce v2
Lodash Chunk vs Native Reduce v3
Lodash Chunk vs Native Chunk
Chunk slice vs. Lodash chunk
Comments
Confirm delete:
Do you really want to delete benchmark?