Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES6 equivalent to lodash _.mapValues 123
(version: 0)
Comparing performance of:
native approach with reduce vs native approach fromEntries vs lodash mapValues
Created:
3 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 } }; console.log(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 } }; console.log(Object.fromEntries( Object.entries(fruits).map(([key, { number }]) => [key, number]) ));
lodash mapValues
var fruits= { 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; console.log(_.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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided benchmark definition and test cases focus on testing the performance of three approaches to transform an object with nested properties into an object with single-value properties: 1. **Native approach using `reduce()`**: This method iterates over the key-value pairs of the original object, applying a transformation function to each value. 2. **Native approach using `Object.fromEntries()` and `map()`**: This method uses the new `fromEntries()` method (introduced in ECMAScript 2017) to create an object from an array of key-value pairs generated by mapping over the original object's entries. 3. **Lodash `mapValues()` function**: A popular utility library that provides a convenient way to transform objects. **Pros and Cons:** * **Native approach using `reduce()`**: * Pros: widely supported, simple to implement, and well-established. * Cons: may have performance overhead due to the additional iteration step. * **Native approach using `Object.fromEntries()` and `map()`**: * Pros: leverages new ECMAScript features for efficient creation of a new object from an array of key-value pairs. This method is also widely supported. * Cons: may not be as straightforward to understand or implement as the native `reduce()` approach, and its performance benefits are still being optimized by modern browsers. * **Lodash `mapValues()` function**: * Pros: provides a convenient way to perform object transformation with minimal code. The Lodash library is widely used, so this method may be familiar to many developers. * Cons: requires the inclusion of an external library, which can add overhead in terms of size and performance. **Library Considerations:** The `mapValues()` function from Lodash is included as a test case using a `<script src>` tag. This approach assumes that the Lodash library is already loaded and available for use in the test environment. If not, users would need to include the library themselves or choose an alternative implementation. **Special JS Features/Syntax:** The provided benchmark definition and test cases do not explicitly mention any special JavaScript features or syntax beyond what's been covered above (e.g., ECMAScript 2017 features like `Object.fromEntries()`). **Alternatives:** If you'd like to explore other approaches for transforming objects, consider the following alternatives: * **Using a custom implementation**: Instead of relying on built-in methods or libraries, you could implement your own function to perform the transformation. * **Other utility libraries**: Depending on your specific needs, you might find alternative utilities in libraries like Underscore.js or Ramda.js that offer similar functionality. These alternatives would require more significant code changes and potentially different execution environments, but they can provide a fresh perspective on optimizing object transformations.
Related benchmarks:
lodash (v4.17.15) map vs Object.keys map
lodash _.map vs native map true version
Ladash map versus ECMA6 map
lodash v4.17.21 map vs Object.keys map
lodash map vs Object.keys map vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?