Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map or Reduce or Lodash GroupBy Rf
(version: 0)
Map or Reduce or Lodash GroupBy Rf
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:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
gp
240292.6 Ops/sec
map
72987.3 Ops/sec
reduce
113069.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark and its components. **Benchmark Definition** The benchmark is defined by two scripts: one for preparation code and another for HTML preparation code (which is null in this case). The script is a JavaScript function that creates an array of random numbers, groups them using either `groupBy` or `map`, or reduces them to a single object with grouped values. **Test Cases** There are three test cases: 1. **gp**: This test case uses the `groupBy` function from Lodash to group the array of random numbers by their floor value. 2. **map**: This test case uses the `Map` data structure in JavaScript to achieve similar grouping as `gp`. 3. **reduce**: This test case uses the `reduce` method on the array of random numbers, grouping the values based on their floor. **Libraries and Functions** * **Lodash**: A popular JavaScript library that provides various utility functions, including `groupBy`. In this benchmark, Lodash is used for the `groupBy` function. * **Map**: The built-in JavaScript data structure used in the `map` test case. It's a hash table-like object that allows efficient key-value pairs. **Options Compared** In each test case, the following options are compared: * **groupBy vs map**: These two approaches use different data structures to group the array of random numbers. + Pros: - `groupBy`: More concise and expressive syntax using Lodash's `groupBy` function. It's also more flexible with its callback function. - `map`: Uses a standard JavaScript feature, making it more accessible to developers familiar with other languages. + Cons: - `groupBy`: Requires an external library (Lodash) for the `groupBy` function. - `map`: May be less efficient due to the overhead of creating and managing a Map data structure. * **reduce vs map**: These two approaches use different methods to achieve grouping. + Pros: - `reduce`: A standard JavaScript method that's more concise and expressive. It also allows for more control over the iteration process. - `map`: Uses a more straightforward approach with a callback function. + Cons: - `reduce`: May be less readable due to the nested callbacks. **Benchmark Results** The latest benchmark results show the execution times per second for each test case: * **gp**: 8939.0654296875 executions per second * **reduce**: 5953.35498046875 executions per second * **map**: 6857.54443359375 executions per second These results suggest that `groupBy` is the fastest approach, followed by `map`, and then `reduce`. However, please note that these results may vary depending on the specific use case and environment.
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?