Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES6 equivalent to lodash _.mapValues 21322
(version: 0)
Comparing performance of:
native approach with reduce vs native approach fromEntries vs lodash mapValues vs myFOR vs myFOR2
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= { __proto__ : null, '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= { __proto__ : null, '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= { __proto__ : null, 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; console.log(_.mapValues(fruits, 'number'))
myFOR
var fruits= { 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; var result = {} for (key in fruits) { result[key] = fruits[key].number } console.log(result)
myFOR2
var fruits= { __proto__ : null, 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; var result = {} for (key in fruits) { fruits[key] = fruits[key].number } console.log(fruits)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
native approach with reduce
native approach fromEntries
lodash mapValues
myFOR
myFOR2
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 benchmarks. **What is tested?** The provided JSON represents a set of benchmarking tests that compare the performance of different approaches to transform an object with nested values into an array of key-value pairs. The test cases use various methods to achieve this transformation, including: 1. Using `Object.entries()` and `reduce()` 2. Using `Object.entries()` and `map()` 3. Using `lodash` library's `mapValues()` function 4. Using a simple `for...in` loop 5. Using a modified version of the `for...in` loop to assign values directly **Options compared** The main options being compared are: * **Native approach with reduce**: Uses `Object.entries()` and `reduce()` to transform the object. * **Native approach fromEntries**: Uses `Object.fromEntries()` (which is a more recent addition to the ECMAScript standard) to transform the object. * **Lodash mapValues**: Uses the `mapValues()` function from the Lodash library to transform the object. * **myFOR**: Uses a simple `for...in` loop to transform the object. * **myFOR2**: A modified version of the `myFOR` approach, which directly assigns values to the original object. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Native approach with reduce**: * Pros: Fast, efficient, and widely supported. * Cons: Requires understanding of `reduce()` function, may be less readable for some developers. 2. **Native approach fromEntries**: * Pros: More modern and concise than `reduce()`, but still relatively fast. * Cons: May not be as widely supported in older browsers or versions of Node.js. 3. **Lodash mapValues**: * Pros: Very readable, maintainable, and easy to understand for developers familiar with Lodash. * Cons: Requires inclusion of the Lodash library, which may add unnecessary overhead. 4. **myFOR**: * Pros: Simple, straightforward, and easy to understand for developers without prior knowledge of object transformations. * Cons: May be slower than other approaches due to the use of a `for...in` loop. **Libraries and syntax** The Lodash library is used in one of the test cases (`lodash mapValues`). Lodash provides a convenient and readable way to perform various operations on objects, including transforming them. The `mapValues()` function takes an object and a function as arguments, applying the function to each key-value pair. There are no special JavaScript features or syntaxes being used in this benchmarking test. All examples use standard ECMAScript syntax. **Alternatives** If you're interested in exploring alternative approaches or optimizations for transforming objects, here are some additional options: * Using `Object.keys()` and `reduce()` * Using a library like Moment.js to handle date transformations * Using a framework like React or Angular to handle state transformations * Optimizing the loop using techniques like memoization or caching Keep in mind that these alternatives may not be directly comparable to the existing test cases, as they might involve additional complexity or dependencies.
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 native map wraig0
Comments
Confirm delete:
Do you really want to delete benchmark?