Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map or Reduce or Lodash GroupBy
(version: 0)
Map or Reduce or Lodash GroupBy
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 arr = [6.1, 4.2, 6.3];
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):
Let's dive into the provided JSON benchmark definition. **Benchmark Definition:** The benchmark is designed to compare three approaches for grouping an array of numbers by their integer values: 1. `groupBy` from Lodash 2. Using a `Map` data structure 3. Using the `reduce` method **Options Compared:** * `groupBy`: uses the Lodash `groupBy` function, which groups elements based on a property or function. * `Map`: uses an instance of the JavaScript `Map` class to store and retrieve values based on their keys (in this case, integer values). * `reduce`: uses the `reduce` method with a custom callback function to group elements. **Pros and Cons:** 1. `groupBy`: * Pros: concise and easy to read, leverages Lodash's functionality. * Cons: depends on the Lodash library being available, may have performance overhead due to function call. 2. `Map`: * Pros: lightweight, efficient, and scalable. * Cons: requires manual key management (integer values), may be slower for large datasets. 3. `reduce`: * Pros: flexible, customizable, and performant. * Cons: requires understanding of the `reduce` method's behavior, can be complex to implement. **Library:** The Lodash `groupBy` function is used in the benchmark definition. Its purpose is to group elements based on a property or function. **Special JS Feature/Syntax:** None mentioned. **Benchmark Preparation Code:** * The script preparation code includes several utility functions for working with arrays and objects, such as `arrayReduce`, `baseFor`, `isArrayLike`, and `baseAssignValue`. * The Lodash `groupBy` function is used to group the input array based on its integer values. * The `Map` implementation uses an instance of the JavaScript `Map` class to store and retrieve values based on their keys. **Other Alternatives:** 1. Using a different grouping library, such as `lodash.groupby` or `underscore.groupBy`. 2. Implementing a custom grouping function using a different approach, such as using a Trie data structure. 3. Using other data structures, like an object with nested arrays or a Set-based approach. Keep in mind that the choice of implementation and library may affect performance, readability, and maintainability of the code.
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?