Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash mapValues
(version: 0)
Comparing performance of:
Lodash vs Native reduce vs native for vs native forEach
Created:
4 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:
users = { 'fred': { 'user': 'fred', 'age': 40 }, 'pebbles': { 'user': 'pebbles', 'age': 1 } };
Tests:
Lodash
_.mapValues(users, ({ age }) => age);
Native reduce
const keys = _.keys(users); keys.reduce((acc, key) => { acc[key] = users[key].age; return acc; }, {});
native for
const keys = _.keys(users); const len = keys.length; const result = {}; for (let index = 0; index < len; index++) { result[keys[index]] = users[keys[index]].age; }
native forEach
const keys = _.keys(users); const result = {}; keys.forEach(key => { result[key] = users[key].age; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash
Native reduce
native for
native forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:136.0) Gecko/20100101 Firefox/136.0
Browser/OS:
Firefox 136 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
12490886.0 Ops/sec
Native reduce
13881254.0 Ops/sec
native for
15034309.0 Ops/sec
native forEach
14098987.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark definition and explain what is being tested. **Overview** The benchmark is testing the performance of four different approaches to iterate over an object and extract values: 1. **Lodash**: Using the `_.mapValues` function from Lodash, a popular JavaScript utility library. 2. **Native Reduce**: Using the built-in `reduce` method on an array of keys. 3. **Native For Loop**: Using a traditional `for` loop to iterate over the keys and extract values. 4. **Native ForEach Loop**: Using the `forEach` method to iterate over the keys. **Options being compared** The benchmark is comparing the performance of these four approaches: * Lodash's `_.mapValues` function * Built-in `reduce` method on an array of keys * Traditional `for` loop * `forEach` method **Pros and Cons** Here are some pros and cons for each approach: 1. **Lodash**: Pros: * Concise and readable code * Can be faster than native methods due to optimized implementation Cons: * Adds dependency on Lodash library, which may not be desirable in all cases * May have additional overhead due to the need to load the Lodash library 2. **Native Reduce**: Pros: * Fast and efficient, since it's a built-in method * Does not add any additional dependencies or overhead Cons: * Requires extra work to create an array of keys * May not be as readable or concise as Lodash code 3. **Native For Loop**: Pros: * Familiar and easy to understand for many developers * Does not require creating an array of keys Cons: * Can be slower due to the overhead of the loop * Requires more code and may be less readable than other approaches 4. **Native ForEach Loop**: Pros: * Similar to traditional `for` loop, but with a more modern syntax * Does not require creating an array of keys Cons: * May be slower due to the overhead of the loop * Less familiar for some developers **Library and its purpose** In this benchmark, Lodash is used as a utility library that provides a `_.mapValues` function. This function takes an object and returns a new object with values mapped from the original object. **Special JS feature or syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other alternatives** If you're interested in exploring alternative approaches, here are some options: * Using `Object.entries()` and `reduce()` to iterate over an object's entries * Using a `Map` data structure to store keys and values * Using a functional programming approach with `map()`, `filter()`, and `reduce()`
Related benchmarks:
Map lodash and PureJS
_.map vs array.map
aadasdsa
A native map vs lodash _.map
Comments
Confirm delete:
Do you really want to delete benchmark?