Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash flatMap vs map reduce
(version: 0)
Comparing performance of:
FlatMap vs Reduce
Created:
one year 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 x = [ { "id": "1", "coordinates": { "latitude": 1, "longitude": 1 }, "cells": [ { "cellGlobalIdentifier": "00001", "id": "0000000001", "locationName": "Loc A Loc B Loc C", "coordinates": { "latitude": 10.000000, "longitude": 10.000000 } } ], "cgis": ["0000100000001"], "laccellIds": ["0000000001"] }, { "id": "10.0001#10.0002", "coordinates": { "latitude": 10.0001, "longitude": 10.0002 }, "cells": [ { "cellGlobalIdentifier": "00002", "lacOrTac_CellId": "0000000002", "locationName": "Loc D Loc E Loc F", "coordinates": { "latitude": 10.0001, "longitude": 10.0002 } } ], "cgis": ["0000200000002"], "laccellIds": ["0000000002"] }, { "id": "10.0003#10.0004", "coordinates": { "latitude": 10.0003, "longitude": 10.0004 }, "cells": [ { "cellGlobalIdentifier": "00003", "lacOrTac_CellId": "0000000003", "locationName": "Loc G Loc H Loc I", "coordinates": { "latitude": 10.0003, "longitude": 10.0004 } } ], "cgis": ["0000300000003"], "laccellIds": ["0000000003"] }, { "id": "10.0005#10.0006", "coordinates": { "latitude": 10.0005, "longitude": 10.0006 }, "cells": [ { "cellGlobalIdentifier": "00004", "lacOrTac_CellId": "0000000004", "locationName": "Loc J Loc K Loc L", "coordinates": { "latitude": 10.0005, "longitude": 10.0006 } } ], "cgis": ["0000400000004"], "laccellIds": ["0000000004"] }, { "id": "10.0007#10.0008", "coordinates": { "latitude": 10.0007, "longitude": 10.0008 }, "cells": [ { "cellGlobalIdentifier": "00005", "lacOrTac_CellId": "0000000005", "locationName": "Loc M Loc N Loc O", "coordinates": { "latitude": 10.0007, "longitude": 10.0008 } } ], "cgis": ["0000500000005"], "laccellIds": ["0000000005"] }, { "id": "10.0009#10.0010", "coordinates": { "latitude": 10.0009, "longitude": 10.0010 }, "cells": [ { "cellGlobalIdentifier": "00006", "lacOrTac_CellId": "0000000006", "locationName": "Loc P Loc Q Loc R", "coordinates": { "latitude": 10.0009, "longitude": 10.0010 } }, { "cellGlobalIdentifier": "00007", "lacOrTac_CellId": "0000000007", "locationName": "Loc S Loc T Loc U", "coordinates": { "latitude": 10.0009, "longitude": 10.0010 } }, { "cellGlobalIdentifier": "00008", "lacOrTac_CellId": "0000000008", "locationName": "Loc V Loc W Loc X", "coordinates": { "latitude": 10.0009, "longitude": 10.0010 } } ], "cgis": ["0000600000006", "0000700000007", "0000800000008"], "laccellIds": ["0000000006", "0000000007", "0000000008"] } ];
Tests:
FlatMap
_.flatMap(x, y => y.cgis)
Reduce
x.map(cellSite => cellSite.cgis).reduce((acc, cgisList) => acc.concat(cgisList), []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
FlatMap
Reduce
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
FlatMap
6702894.0 Ops/sec
Reduce
2977935.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition** The provided JSON represents a benchmark definition, which describes two test cases: 1. `_.flatMap(x, y => y.cgis)`: This is a Lodash function call that flattens an array of objects, applying a transformation to each object in the array. 2. `x.map(cellSite => cellSite.cgis).reduce((acc, cgisList) => acc.concat(cgisList), [])`: This is a custom implementation using the built-in `map` and `reduce` functions. **Options Compared** The two options compared are: 1. Lodash's `_.flatMap` 2. Custom implementation using `map` and `reduce` **Pros and Cons of Each Approach** **Lodash's `_.flatMap`** Pros: * Optimized for performance by the Lodash team * Handles edge cases (e.g., empty arrays, null values) automatically Cons: * Adds an external dependency on the Lodash library * May not provide direct insight into the underlying algorithm **Custom Implementation using `map` and `reduce`** Pros: * No external dependencies required * Provides a clear understanding of the underlying algorithm Cons: * Requires more manual optimization and edge case handling * May be slower due to overhead from function calls and loop iterations **Library Used - Lodash** Lodash is a popular utility library for JavaScript that provides a wide range of functions, including `_.flatMap`. The library is designed to optimize common operations and provide a consistent API across different browsers and environments. **Latest Benchmark Result** The provided benchmark result shows the execution time per second for each test case on a specific device (Chrome 125, Mac OS X 10.15.7). The results indicate that: * Lodash's `_.flatMap` is faster than the custom implementation using `map` and `reduce`, with an execution time of approximately 6702894 executions per second. * The custom implementation has an execution time of approximately 2977935 executions per second. In conclusion, while both options have their pros and cons, Lodash's `_.flatMap` appears to be the faster choice for this specific benchmark. However, if you're interested in understanding the underlying algorithm or want more control over optimization, the custom implementation might be worth exploring further.
Related benchmarks:
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries [2]
lodash map vs native map wraig0
lodash entries vs object entries
Comparison of flatmap
Comments
Confirm delete:
Do you really want to delete benchmark?