window.data = [];
for (let i = 0; i < 10000; i++) {
data.push({
key: `key${i}`
});
}
window.dataBig = [];
for (let i = 0; i < 100000; i++) {
dataBig.push({
key: `key${i}`
});
}
const keyed = _.keyBy(data, v => v.key)
const map = new Map(data.map((o) => [o.key, o]));
const keyed = _.keyBy(dataBig, v => v.key)
const map = new Map(dataBig.map((o) => [o.key, o]));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash keyBy (10k entries) | |
JS Map (10k entries) | |
Lodash keyBy (100k entries) | |
JS Map (100k entries) |
Test name | Executions per second |
---|---|
Lodash keyBy (10k entries) | 2310.2 Ops/sec |
JS Map (10k entries) | 1808.9 Ops/sec |
Lodash keyBy (100k entries) | 163.3 Ops/sec |
JS Map (100k entries) | 165.1 Ops/sec |
The benchmark compares two different methods for creating a keyed collection from an array of objects, specifically assessing the performance of Lodash's keyBy
function against the native JavaScript Map
constructor. The tests are conducted with two different sizes of data: 10,000 and 100,000 entries.
Lodash keyBy
const keyed = _.keyBy(data, v => v.key)
key
property of each object). Lodash manages iteration and data manipulation, making it cleaner and often simpler to read.JavaScript Map
const map = new Map(data.map((o) => [o.key, o]));
Map
constructor. Here, it first transforms the array into an array of key-value pairs (where the key comes from the object's key
property and the value is the object itself), and then passes these pairs to the Map
constructor to create a keyed collection.Pros:
Cons:
Pros:
Map
constructor, being built into the language, typically offers better performance, especially for larger datasets.Cons:
Execution Speed: In the benchmark results, for the 10,000 entries test, Lodash keyBy
performed better (2310.20 executions per second) than the JS Map
method (1808.89 executions per second). However, the performance gap narrows significantly as the dataset increases, with the JS Map (165.05 executions per second) outperforming Lodash's method (163.29 executions per second) at 100,000 entries. This suggests that for larger datasets, the optimized architecture of native JavaScript constructs can lead to better performance.
Use Case Appropriateness: Choosing between these two methods can depend on the context. If performance is critical and the dataset is significantly large, native JavaScript (Map
) may be the preferred choice. Conversely, for smaller datasets or situations where development speed and code clarity are prioritized, Lodash may be beneficial.
Other alternatives to keyBy
and Map
for creating keyed collections could include using plain objects, though they lack some advantages of Map
, such as key ordering and object reference capabilities. There are also other libraries similar to Lodash that offer similar functionalities like Ramda, but they may also introduce their own pros and cons regarding performance and complexity.
In summary, this benchmark serves as a valuable reference for performance-critical decisions in JavaScript code dealing with keyed collections. Developers should weigh the implications of performance, readability, and dependency management when choosing between Lodash and native JavaScript options.