Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map or GroupBy
(version: 0)
Map or GroupBy
Comparing performance of:
gp vs map
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) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
gp
map
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):
I'll break down the provided benchmark definition and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Definition** The benchmark is designed to measure the performance of mapping or grouping operations on an array. There are two test cases: 1. `map`: Uses a `Map` data structure to store the grouped elements. 2. `gp` (short for "groupBy"): Uses a `groupBy` function that returns an object with grouped elements. **Options Compared** The benchmark compares two approaches: 1. Using a `Map` data structure (`map` test case) 2. Implementing a custom grouping algorithm using a `groupBy` function (`gp` test case) **Pros and Cons of Each Approach** **Map Approach (map)** Pros: * Efficient use of memory, as each element is stored in a separate entry in the map. * Fast lookup and insertion operations. Cons: * May require more memory to store the entire map. * May have slower startup times due to map initialization. **Custom Grouping Algorithm (gp)** Pros: * More control over the grouping process. * Can potentially be optimized for specific use cases. Cons: * Requires manual implementation of the grouping algorithm, which can be error-prone and time-consuming. * May require more memory to store intermediate results. * Lookup and insertion operations may be slower due to the custom implementation. **Other Considerations** * The `Map` data structure is implemented as an object with numeric keys, where each key corresponds to a unique element in the input array. This allows for fast lookup and insertion operations. * The `groupBy` function returns an object with grouped elements, where each key corresponds to a unique group (i.e., a subset of the input array that shares a common property). **Library and Special JS Features** The `Map` data structure is a built-in JavaScript object. The `groupBy` function uses this object to store the grouped elements. There are no special JavaScript features used in this benchmark, aside from the standard ECMAScript syntax for working with objects and arrays. **Alternatives** Other alternatives for implementing group-by operations include: * Using an array of arrays, where each inner array represents a group. * Using a data structure like a heap or balanced tree to store the grouped elements. * Implementing a custom sorting algorithm to group elements based on specific criteria.
Related benchmarks:
Object.values vs for in loop
for loop: object vs map v2
for loop: object vs map v3
map vs for loop to identify multi inputs borys v1
Take two arrays and merge them using an object key (Map vs. object)
Comments
Confirm delete:
Do you really want to delete benchmark?