Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES6 equivalent to lodash _.mapValues without console.log
(version: 0)
Comparing performance of:
native approach with reduce vs native approach fromEntries vs lodash mapValues
Created:
5 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 dive into the benchmark. **Overview** The provided JSON represents a JavaScript microbenchmark test case for measuring the performance of different approaches to map values in an object. The test consists of three individual benchmark definitions, each comparing two methods: a native approach using `reduce`, another native approach using `fromEntries` with `map`, and a third approach using the Lodash library's `mapValues`. **Native Approaches** 1. **`native approach with reduce`**: This method uses `reduce` to iterate over the object entries, mapping each key-value pair to the value (in this case, the "number" property). * Pros: Generally efficient and easy to implement. * Cons: Can be slower for large datasets due to the overhead of the `reduce` function. Additionally, it may require more memory allocations than necessary. 2. **`native approach fromEntries`**: This method uses the `fromEntries` method to create a new array of key-value pairs and then maps each pair to an object with only the "number" property. * Pros: May be faster than the `reduce` approach, especially for large datasets, as it avoids the overhead of function calls. However, it creates an additional array of objects, which may require more memory allocations. * Cons: The `fromEntries` method itself is a relatively expensive operation. **Lodash Approach** 1. **`lodash mapValues`**: This method uses the Lodash library's `mapValues` function to create a new object with values mapped from the original object. * Pros: Generally efficient and easy to implement, as it leverages an optimized implementation in the Lodash library. * Cons: Requires including the Lodash library in the test, which may add overhead. Additionally, if not used carefully, it can lead to slower performance due to unnecessary function calls. **Other Considerations** * The use of `Object.entries` and `Object.fromEntries` methods is specific to JavaScript versions 26 and above. * The Safari browser version (16) seems to be quite efficient in executing the benchmark tests. This might indicate that other browsers or versions could produce different results. **Alternatives** In addition to these three approaches, other possible methods for mapping values in an object include: * Using a `forEach` loop with a callback function * Utilizing modern JavaScript features like `for...of` loops and arrow functions * Leveraging the `Object.assign()` method (although this might be less efficient than the native approaches) However, these alternatives are not explicitly tested on MeasureThat.net.
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?