Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash chain big_data
(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:
const col = ['blue', 'yellow','green','notgreen','black']; const templateData = { "name": "jim", "color": "blue", "age": "22" }; function generateDataArray(template, count) { const dataArray = []; for (let i = 0; i < count; i++) { const newData = { ...template, name: template.name + "_" + i, age: template.age + i, color: col[Math.floor(Math.random() * col.length)] }; dataArray.push(newData); } return dataArray; } var data = generateDataArray(templateData, 1000000); 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):
Let's break down the benchmark definition and explain what is being tested, along with the pros and cons of different approaches. **Benchmark Definition** The test case measures the performance of two functions: `chain` and `custom`. Both functions are used to group the elements of an array based on a specific property (`color`) using the Lodash library. **What is being tested?** * The performance of applying the `_groupBy` function from Lodash directly, as in the `custom` function. * The performance of composing multiple Lodash functions together using the `_mapValues` function from Lodash, as in the `chain` function. **Lodash library and its purpose** Lodash is a JavaScript utility library that provides various functional programming helpers. In this case, it's used for array manipulation and grouping. Specifically: + `_groupBy`: groups an array by a given property. + `_map`: maps over an array and applies a transformation to each element. + `_mapValues`: maps the values of an object (in this case, the result of `_groupBy`) to new objects. **Options compared** The two functions being tested differ in how they apply Lodash's grouping functionality: 1. **Direct approach (`custom` function)**: Uses `_.groupBy(x, 'color')`, which applies the groupBy function directly to the input array. 2. **Chained approach (`chain` function)**: Uses `_mapValues(chainableFunctions, ...)` to compose multiple functions together, where each function is applied in sequence. **Pros and cons of different approaches** 1. **Direct approach (`custom` function)**: * Pros: easier to read and understand, as the grouping logic is explicit. * Cons: may lead to slower performance due to the overhead of direct function calls. 2. **Chained approach (`chain` function)**: * Pros: can be more efficient if the functions are composed correctly, as it avoids the overhead of direct function calls. * Cons: requires more code and a deeper understanding of functional programming concepts. **Special JS features or syntax** The `chain` function uses a technique called "function currying," which allows for partial application of functions. This is achieved by using `_mapValues` with an object that maps each Lodash function to a new function that takes the input as its first argument. In contrast, the `custom` function directly applies `_.groupBy` and then `_.map`, without any composition or currying. **Other alternatives** If not using Lodash, other libraries like Ramda or Underscore.js could be used for similar array manipulation tasks. However, their APIs might differ significantly from Lodash's, requiring additional learning and adaptation. Keep in mind that the performance difference between these approaches may depend on various factors, such as the specific use case, input data distribution, and JavaScript engine optimizations.
Related benchmarks:
Lodash chunk vs JS
Lodash chunk vs JS 2
Lodash vs reduce Map()
lodash groupBy vs Array.reduce 100k better 2
lodash groupBy vs Array.reduce vs simple for loop
Comments
Confirm delete:
Do you really want to delete benchmark?