Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash/keyBy vs native reduce #3
(version: 0)
Comparing performance of:
lodash/keyBy vs keyBy with native reduce vs keyBy with forEach vs keyBy with for loop
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var data = [ { name: 'pi', value: 3.14 }, { name: 'hundred', value: 100 }, { name: 'truthy', value: true }, { name: 'falsy', value: false }, { name: 'estring', value: '2.71828' }, ]; for(let i = 0; i < 200; i++) { data.push({ name: 'estring', value: '2.71828' }); } var keyBy = (array, fn) => { return array.reduce((acc, value) => { acc[fn(value)] = value; return acc; }, {}); }; var keyByForEach = (array, fn) => { var acc = {}; _.forEach(array, (value) => { acc[fn(value)] = value; }); return acc; }; var keyByForLoop = (array, fn) => { var acc = {}; for (let i = 0; i < array.length; i++) { acc[fn(array[i])] = array[i]; } return acc; };
Tests:
lodash/keyBy
_.keyBy(data, (v) => v.name);
keyBy with native reduce
keyBy(data, (v) => v.name);
keyBy with forEach
keyByForEach(data, (v) => v.name);
keyBy with for loop
keyByForLoop(data, (v) => v.name);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash/keyBy
keyBy with native reduce
keyBy with forEach
keyBy with for loop
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 explanation of the benchmark. **Benchmark Overview** The benchmark measures the performance of three different approaches to create a key-value mapping from an array of objects, where each object has a unique value in its `name` property. The approaches are: 1. Lodash's `keyBy` function 2. A native implementation using `Array.prototype.reduce()` (also known as "native reduce") 3. Another approach that uses a custom `forEach` loop (`keyByForEach`) 4. An alternative approach that uses a traditional `for` loop (`keyByForLoop`) **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for various tasks, including data manipulation, string manipulation, and more. The `keyBy` function in this benchmark takes an array of objects and a mapping function as input, returning a new object where each key is the mapped value from the original array. **Native Reduce** The native implementation uses `Array.prototype.reduce()` to create the key-value mapping. This approach is a built-in JavaScript method that applies a reduction function to each element in the array, accumulating a result. In this case, it's used to create an object where each key is the mapped value from the original array. **Pros and Cons** * Native Reduce: + Pros: Efficient, lightweight, and widely supported. + Cons: Requires familiarity with JavaScript array methods and may be less readable than other approaches. * Lodash `keyBy`: + Pros: Readable, maintainable, and easy to use. + Cons: Adds external dependency (Lodash), may have performance overhead due to function calls. **Other Alternatives** There are a few other approaches that could be used to create this key-value mapping: * Using `Array.prototype.forEach()` with an object initialization * Using a custom implementation without relying on JavaScript array methods However, these alternatives are not included in the benchmark, as they were deemed less relevant or efficient. **Special JS Features/Syntax** None of the approaches use special JavaScript features or syntax that would affect their performance. The benchmark focuses on comparing different algorithmic approaches to data processing. Let me know if you'd like more details or clarification on any aspect of the benchmark!
Related benchmarks:
lodash vs for-of vs forEach5453
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values
Loop over object: lodash vs Object.entries vs Object.values vs Object.keys (lodash 4.17.15)
Loop over object: lodash.forOwn vs Object.keys().forEach
Comments
Confirm delete:
Do you really want to delete benchmark?