Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES6 equivalent to lodash _.mapValues 3
(version: 0)
Comparing performance of:
native approach with reduce vs native approach fromEntries vs lodash mapValues
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://raw.githubusercontent.com/lodash/lodash/4.17.15-npm/core.js"></script>
Tests:
native approach with reduce
var fruits= { 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; Object.entries(fruits) .reduce((a, [key, { number }]) => { a[key] = number; return a; }, {} );
native approach fromEntries
var fruits= { 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; Object.fromEntries( Object.entries(fruits).map(([key, { number }]) => [key, number]) );
lodash mapValues
var fruits= { 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; _.mapValues(fruits, 'number');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
native approach with reduce
native approach fromEntries
lodash mapValues
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 the provided benchmark and explain what is tested, the options compared, their pros and cons, and other considerations. **Benchmark Overview** The benchmark tests three different approaches to transform an object with nested values into an object with only numeric values. The input data structure is a JavaScript object `fruits` with two properties: `'apple'` and `'orange'`, each containing another object with a single key-value pair (`'name'` and `'number'`). The benchmark aims to determine which approach performs better in terms of speed. **Options Compared** The three approaches compared are: 1. **Native Approach using Reduce**: This method uses the `reduce()` function to iterate over the object's entries, and for each entry, it updates the corresponding key in the resulting object with the value from the nested object. 2. **Native Approach using fromEntries**: This method uses the `fromEntries()` function to create a new array of key-value pairs from the object's entries, maps each pair to extract only the numeric value, and then creates a new object from this array. 3. **Lodash `mapValues`**: This method uses the Lodash library to transform the input object by mapping over its values (in this case, objects with a single key-value pair) and returning an object with the same keys but new values. **Pros and Cons of Each Approach** 1. **Native Approach using Reduce**: * Pros: Native code, potential for better performance since it doesn't require additional library calls. * Cons: Can be more complex to implement, especially for those unfamiliar with `reduce()`. 2. **Native Approach using fromEntries**: * Pros: Simplifies the implementation by using a built-in function, easier to understand for those familiar with array manipulation. * Cons: Requires an additional library call (`fromEntries()`), which might introduce overhead. 3. **Lodash `mapValues`**: * Pros: Convenient and straightforward, leverages a popular and well-maintained library. * Cons: Requires an additional library call (Lodash) and may incur performance overhead due to the abstraction layer. **Other Considerations** * The use of Lodash's `mapValues` method might skew the results if the benchmarking environment is not optimized for its usage or has limited resources. * The Native Approach using Reduce and fromEntries methods rely on internal JavaScript functions, which should be relatively consistent across implementations. However, this consistency may vary between browsers or Node.js versions. **Library Used** The Lodash library is used in the "Lodash mapValues" test case. Lodash is a popular utility library for JavaScript that provides a comprehensive set of functional programming helpers. In this benchmark, `mapValues` is used to transform the input object by mapping over its values and returning an object with the same keys but new values. **Special JS Feature/ Syntax** None are explicitly mentioned in this benchmark definition. In summary, the benchmark compares three approaches to transform an object with nested values into an object with only numeric values. The Native Approach using Reduce and fromEntries methods are native JavaScript functions that provide a straightforward implementation, while Lodash's `mapValues` method simplifies the transformation but requires an additional library call.
Related benchmarks:
lodash (v4.17.15) map vs Object.keys map
lodash vs es6 map
array.map vs _.map
lodash _.map vs native map true version
Ladash map versus ECMA6 map
Comments
Confirm delete:
Do you really want to delete benchmark?