Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash/keyBy vs native reduce, large dataset
(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<1000; i++){ data.push({ name: `i-${i}`, value: i + (i*10) + (i*100), junk1: 'junk', junk2: 123, junk3: 'morejunk', junk4: 'evenmore', junk5: 345, junk6: 789, junk7: 'blah', junk8: 'baasdsadsadassdsadasdsa dsasdfdsfaascacf afaw', junk9: 'badadadadadadad', junk10: false, junk11: true, junk12: null }) } 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 break down what is being tested in this benchmark. **Benchmark Overview** The benchmark tests the performance of three different approaches to create a key-value mapping from an array of objects: 1. `keyBy` (using Lodash) 2. `keyBy with native reduce` 3. `keyBy with forEach` All three approaches take an array of objects and a function as input, which is used to extract the key value from each object. **Options Compared** Here's what each option does: 1. **Lodash `keyBy`**: This approach uses Lodash's `keyBy` function, which creates a hash map (an object where keys are unique) by iterating over the array and using the provided function to extract the key value from each object. 2. **Native `reduce`**: This approach uses the built-in `reduce` method of JavaScript arrays, which applies a function to each element in the array and reduces it to a single output value (in this case, an object). 3. **Native `forEach`**: This approach uses the built-in `forEach` method of JavaScript arrays, which executes a callback function for each element in the array. 4. **Native `for loop`**: This approach uses a traditional `for` loop to iterate over the array and create the key-value mapping. **Pros and Cons** Here are some pros and cons of each approach: 1. **Lodash `keyBy`**: * Pros: Efficient, concise code; Lodash's implementation is optimized for performance. * Cons: Requires an external library (Lodash); may not be familiar to developers who don't use Lodash. 2. **Native `reduce`**: * Pros: Built-in, no external dependencies; can be more efficient than other approaches. * Cons: Can be less readable and maintainable due to the complexity of the implementation. 3. **Native `forEach`**: * Pros: Easy to implement; uses built-in methods that are widely recognized. * Cons: May be slower than `reduce` due to the overhead of iterating over the array using a callback function. 4. **Native `for loop`**: * Pros: Simple, easy to understand; no external dependencies. * Cons: Can be slow and less efficient than other approaches. **Library Usage** The benchmark uses Lodash's `keyBy` function, which is a popular utility library for JavaScript. The purpose of this library is to provide a convenient way to create hash maps or key-value pairs from arrays of objects. **Special JS Features/Syntax** This benchmark does not use any special JavaScript features or syntax that are specific to certain browsers or versions. However, it's worth noting that the `for` loop approach may be less efficient than other approaches due to the overhead of creating and iterating over a traditional `for` loop. **Alternatives** Other alternatives for creating key-value mappings from arrays of objects include: * Using a library like Moment.js or Date-fns for date-related data * Implementing a simple hash map using an object literal (e.g., `{}`) with numerical keys * Using a different programming paradigm, such as functional programming with `map` and `reduce` Overall, this benchmark provides a useful comparison of different approaches to creating key-value mappings from arrays of objects, highlighting the trade-offs between conciseness, performance, and maintainability.
Related benchmarks:
lodash vs for-of vs forEach5453
lodash groupBy vs Array.reduce (2)
lodash groupBy vs Array.reduce 100k with array push
flatMap vs reduce (push)
flatMap vs reduce-push vs flat
Comments
Confirm delete:
Do you really want to delete benchmark?