Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map or Reduce or Lodash GroupBy R
(version: 0)
Map or Reduce or Lodash GroupBy R
Comparing performance of:
gp vs map vs reduce
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var MAX_SAFE_INTEGER = 9007199254740991 function arrayReduce(array, iteratee, accumulator, initAccum) { let index = -1 const length = array == null ? 0 : array.length if (initAccum && length) { accumulator = array[++index] } while (++index < length) { accumulator = iteratee(accumulator, array[index], index, array) } return accumulator } function baseFor(object, iteratee, keysFunc) { const iterable = Object(object) const props = keysFunc(object) let { length } = props let index = -1 while (length--) { const key = props[++index] if (iteratee(iterable[key], key, iterable) === false) { break } } return object } function isLength(value) { return typeof value === 'number' && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER } function isArrayLike(value) { return value != null && typeof value !== 'function' && isLength(value.length) } function baseForOwn(object, iteratee) { return object && baseFor(object, iteratee, keys) } function baseEach(collection, iteratee) { if (collection == null) { return collection } if (!isArrayLike(collection)) { return baseForOwn(collection, iteratee) } const length = collection.length const iterable = Object(collection) let index = -1 while (++index < length) { if (iteratee(iterable[index], index, iterable) === false) { break } } return collection } function baseReduce(collection, iteratee, accumulator, initAccum, eachFunc) { eachFunc(collection, (value, index, collection) => { accumulator = initAccum ? (initAccum = false, value) : iteratee(accumulator, value, index, collection) }) return accumulator } function reduce(collection, iteratee, accumulator) { const func = Array.isArray(collection) ? arrayReduce : baseReduce const initAccum = arguments.length < 3 return func(collection, iteratee, accumulator, initAccum, baseEach) } var hasOwnProperty = Object.prototype.hasOwnProperty function baseAssignValue(object, key, value) { if (key == '__proto__') { Object.defineProperty(object, key, { 'configurable': true, 'enumerable': true, 'value': value, 'writable': true }) } else { object[key] = value } } function groupBy(collection, iteratee) { return reduce(collection, (result, value, key) => { key = iteratee(value) if (hasOwnProperty.call(result, key)) { result[key].push(value) } else { baseAssignValue(result, key, [value]) } return result }, {}) } var n = 730; var arr = [...Array(n)].map((_) => Math.random() * 10);
Tests:
gp
const lm = groupBy(arr, Math.floor)
map
const m = new Map(); for (const v of arr) { const k = Math.floor(v); const temp = m.get(k) ?? []; temp.push(v); m.set(k, temp) }
reduce
const rm = arr.reduce((acc, v) => { const k = Math.floor(v) + ''; (acc[k] || (acc[k] = [])).push(v) return acc; }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
gp
map
reduce
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):
**Benchmark Overview** The provided JSON represents a benchmark test suite for JavaScript microbenchmarks on MeasureThat.net. The test suite consists of three individual test cases: `gp`, `map`, and `reduce`. These test cases are designed to measure the performance of different approaches for grouping or reducing an array. **Test Case 1: gp (Group By)** The first test case uses the `groupBy` function from the Lodash library, which groups the input array by a specified iteratee. The `iteratee` is set to `Math.floor`, meaning that each element in the array will be mapped to its floor value as the group key. * **Approach:** Using the `groupBy` function from Lodash. * **Pros:** + Convenient and concise syntax. + Efficient implementation, leveraging the underlying library's optimization. * **Cons:** + Additional dependency on the Lodash library. + May not be suitable for all use cases, as it relies on the specific iteration order of the input array. **Test Case 2: map (Map)** The second test case uses a manual implementation of a map data structure to group the input array by its floor value. This approach is more verbose than using `groupBy`, but provides full control over the grouping process. * **Approach:** Manual implementation of a map data structure. * **Pros:** + No additional dependencies or libraries required. + Full control over the grouping process and iteration order. * **Cons:** + More verbose syntax and code. + May be less efficient due to manual implementation details. **Test Case 3: reduce (Reduce)** The third test case uses a reduction function, similar to `reduce`, but with a custom iteratee that maps each element's floor value as the group key. This approach is more concise than the manual map implementation. * **Approach:** Custom reduction function. * **Pros:** + Concise syntax and code. + Efficient implementation, leveraging the underlying library's optimization. * **Cons:** + Additional dependency on the `reduce` method from JavaScript libraries or frameworks. + May not be suitable for all use cases, as it relies on the specific iteration order of the input array. **Benchmark Results** The latest benchmark results show that: 1. The `gp` (Group By) test case performs best with an average execution rate of 15046.88 executions per second (EPS). 2. The `map` (Map) test case has a lower average EPS of 10836.14. 3. The `reduce` (Reduce) test case has the lowest average EPS of 9354.54. These results indicate that using the built-in `groupBy` function from Lodash provides the best performance for this specific benchmark, while offering more control and flexibility in terms of implementation. However, other approaches like manual map implementation or custom reduction functions may be preferred depending on specific use cases or requirements.
Related benchmarks:
Test length assign
Test length assign 100k
Test length assign 1000
fromEntries vs reduce 2
map-values
Comments
Confirm delete:
Do you really want to delete benchmark?