Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Map or Reduce or Lodash GroupBy Rf
Map or Reduce or Lodash GroupBy Rf
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:147.0) Gecko/20100101 Firefox/147.0
Browser:
Firefox 147
Operating system:
Mac OS X 10.15
Device Platform:
Desktop
Date tested:
2 months ago
Test name
Executions per second
gp
240292.6 Ops/sec
map
72987.3 Ops/sec
reduce
113069.1 Ops/sec
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; }, {})