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]
const list = [_list]
const c = chunkR(list, 200)
if (c.length != 5000) throw new Error('bad result')
const list = [_list]
const c = chunkL(list, 200)
if (c.length != 5000) throw new Error('bad result')
const list = [_list]
const c = chunkL(list, 200)
if (c.length != 5000) throw new Error('bad result')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
splice Recurcive | |
splice While | |
lodash chunk |
Test name | Executions per second |
---|---|
splice Recurcive | 2.1 Ops/sec |
splice While | 37.0 Ops/sec |
lodash chunk | 35.5 Ops/sec |
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
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.chunk
function: This approach uses the lodash.chunk
function, which is designed specifically for chunking arrays.Pros and Cons
splice
:splice()
.chunk
function: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:
Array.prototype.slice()
to create a copy of the array before chunking it.Map
data structure to efficiently chunk the array without modifying the original array.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.