Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash/keyBy vs native map
(version: 0)
Comparing performance of:
lodash/keyBy vs map
Created:
2 years ago
by:
Registered User
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:
var data = [ { name: 'pi', value: 3.14 }, { name: 'hundred', value: 100 }, { name: 'truthy', value: true }, { name: 'falsy', value: false }, { name: 'estring', value: '2.71828' }, ]; var mapkey = (array, fn) => { return new Map(array.map(i => [fn(i), i])); }
Tests:
lodash/keyBy
_.keyBy(data, (v) => v.name);
map
mapkey(data, (v) => v.name);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash/keyBy
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 15 on iOS 15.5
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash/keyBy
7442611.0 Ops/sec
map
2467604.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches for creating a map (an object with key-value pairs) from an array: 1. Using Lodash's `keyBy` function 2. Creating a native Map using a custom `mapkey` function **Options Compared** The two options are compared in terms of performance, which is measured by the number of executions per second. **Pros and Cons of Each Approach** **Lodash's `keyBy` Function:** Pros: * Simpler to write and maintain * Lodash provides a well-tested and maintained implementation Cons: * May have overhead due to the library itself * May not be optimized for performance, especially for large datasets **Native Map using `mapkey` Function:** Pros: * Customizable and can be optimized for specific use cases * No external library dependency * Can potentially outperform Lodash's implementation if well-optimized Cons: * Requires more manual effort to write and maintain * May require additional memory allocation, which can lead to performance issues in some cases **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and object transformation. The `keyBy` function is part of Lodash's "utility" module and is used to create an object with key-value pairs from an array. **Custom `mapkey` Function** The custom `mapkey` function creates a Map from an array using the `Array.prototype.map()` method and then converts it to a Map using the `Map` constructor. The callback function `(v) => v.name` is applied to each element in the array, which extracts the name property as the key. **Special JS Feature/ Syntax: None** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** If you're interested in exploring alternative approaches for creating maps from arrays, here are a few options: 1. Using `Array.prototype.reduce()`: Instead of using `map()` and `Map` constructors, you can use the `reduce()` method to create an object with key-value pairs. 2. Using `Array.prototype.forEach()`: Similar to `reduce()`, you can use the `forEach()` method to iterate over the array and create a map. 3. Using a library like `fast-queue` or `pako`: These libraries provide optimized functions for creating maps from arrays. However, keep in mind that these alternatives may not offer significant performance improvements compared to using the native `Map` constructor or Lodash's `keyBy` function.
Related benchmarks:
Object.keys vs lodash _.keys
lodash (v4.17.15) map vs Object.keys map
Array.prototype.map vs Lodash.map on large data
array.map vs _.map
lodash map vs native map wraig0
Comments
Confirm delete:
Do you really want to delete benchmark?