Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash chain111
(version: 0)
Comparing performance of:
chain vs custom
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js
Script Preparation code:
function generateDataArray(template, count) { const dataArray = []; for (let i = 0; i < count; i++) { const newData = { ...template, name: template.name + "_" + i, }; dataArray.push(newData); } return dataArray; } const templateData = { "name": "jim", "color": "blue", "age": "22" }; var data = generateDataArray(templateData, 100000); var chainableFunctions = { map: _.map, groupBy: _.groupBy, value: _.value, }; var chain = (input) => { var value = input; var wrapper = { ..._.mapValues( chainableFunctions, (f) => (...args) => { // lodash always puts input as the first argument value = f(value, ...args); return wrapper; }, ), value: () => value, }; return wrapper; };
Tests:
chain
console.log( chain(data) // Group the elements of Array based on `color` property .groupBy("color") // `key` is group's name (color), `value` is the array of objects .map((value, key) => ({ color: key, users: value })) .value() );
custom
var custom = (x) => { var group = _.groupBy(x, 'color'); return _.map(_.entries(group), ([ color, val ]) => ({ color, val })) } console.log(custom(data))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
chain
custom
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmark created using the MeasureThat.net website. The benchmark is designed to compare the performance of two different approaches for grouping an array of objects based on a specific property. **Approaches Compared** 1. **Lodash Chain**: This approach uses the Lodash library to create a chainable function. It defines a `chain` function that takes input data and returns a wrapper object with several methods, including `map`, `groupBy`, and `value`. The `groupBy` method groups the elements of the array based on the `color` property, and the resulting grouped objects are returned by the `value` method. 2. **Custom Implementation**: This approach uses a custom function that takes input data and returns a grouped object directly. It uses the `groupBy` method from Lodash to group the elements of the array based on the `color` property. **Pros and Cons** * **Lodash Chain**: + Pros: Encapsulates the logic for grouping and mapping in a single, reusable function. Can be easily composed with other chainable functions. + Cons: Requires loading the Lodash library, which can introduce additional overhead. * **Custom Implementation**: + Pros: Does not require loading an external library, making it potentially faster. + Cons: Requires defining a custom grouping logic, which can be more error-prone and less reusable. **Library and Purpose** The `lodash` library is used extensively in the benchmark. It provides various utility functions for tasks such as array manipulation, string manipulation, and object manipulation. In this specific case, Lodash's `groupBy` function is used to group the elements of an array based on a specific property (in this case, the `color` property). **Special JS Features or Syntax** The benchmark uses several special features of JavaScript: * **Template literals**: Used in the `templateData` object to define a JSON-like data structure. * **Arrow functions**: Used in the `chain` function to create a concise and readable implementation. Overall, this benchmark provides a simple and straightforward way to compare the performance of two different approaches for grouping an array of objects based on a specific property.
Related benchmarks:
Loop over object: lodash vs Object.entries 2
Lodash vs reduce Map()
lodash mapValues vs vanilla Object.keys foreach vs lodash reduce
lodash map vs native map wraig0
test123_123
Comments
Confirm delete:
Do you really want to delete benchmark?