Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash.mapKeys vs Iterating over Object.keys
(version: 0)
Comparing performance of:
lodash.mapKeys vs native
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var value = {a: 30310, b: 100303, c: 3040494};
Tests:
lodash.mapKeys
_.mapKeys(value, k => k + '-changed')
native
Object.assign({}, ...Object.keys(value).map(k => ({ [k + '-changed']: value[k] })))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash.mapKeys
native
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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash.mapKeys
8313428.5 Ops/sec
native
3403516.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark definition and options being compared. **Benchmark Definition:** The benchmark is comparing two approaches to iterate over an object and create a new object with modified key-value pairs: 1. **Lodash `_.mapKeys`**: This function takes an object, a callback function, and returns a new object with the same keys but modified according to the callback. 2. **Native JavaScript iteration**: This approach uses `Object.keys()` to get an array of keys from the original object, maps over it using `Array.prototype.map()`, creates a new object using `Object.assign()`, and updates each key-value pair. **Options being compared:** * Lodash `_.mapKeys` * Native JavaScript iteration **Pros and Cons of each approach:** 1. **Lodash `_.mapKeys`**: * Pros: + More concise and readable code + Less prone to errors due to the callback function providing a clear contract for modification * Cons: + Requires an additional library (Lodash) + May have performance overhead due to the creation of a new function object on each execution 2. **Native JavaScript iteration**: * Pros: + No external libraries required + Can be optimized for performance by using native functions and caching intermediate results * Cons: + More verbose code, potentially leading to errors due to complex logic + May require more manual bookkeeping (e.g., managing the array of keys) **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as data manipulation, object creation, and more. In this benchmark, `_.mapKeys` is used to create a new object with modified key-value pairs. **Special JS feature or syntax: None mentioned** The benchmark does not use any special JavaScript features or syntax, so there's no need to explain anything in that regard. **Other alternatives** If you're interested in exploring alternative approaches, some options could include: * Using `Object.entries()` and `Array.prototype.reduce()` for a more concise solution * Implementing your own custom mapping function using recursion or iteration * Utilizing other libraries like `ramda` or `functional-programming` frameworks Keep in mind that each alternative approach will have its pros and cons, and the best choice depends on the specific requirements of your project. In terms of benchmark results, the provided data shows: * Firefox 124 with an execution rate of approximately 4.79 million iterations per second for Lodash `_.mapKeys` * Firefox 124 with an execution rate of approximately 2.78 million iterations per second for native JavaScript iteration These results suggest that Lodash `_.mapKeys` is currently performing better than the native JavaScript iteration approach in this benchmark, but keep in mind that performance can vary depending on the specific use case and environment.
Related benchmarks:
Object.keys vs lodash _.keys
lodash 4.17.2 map vs Object.keys map
lodash (v4.17.15) map vs Object.keys map
lodash 4.17.15 map vs Object.keys map
Comments
Confirm delete:
Do you really want to delete benchmark?