Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
GroupBy
(version: 15)
Lodash vs Ramda
Comparing performance of:
Lodash groupBy vs Ramda groupBy vs Hand written vs Using Reduce
Created:
8 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Script Preparation code:
function Rand(x) { return Math.floor(Math.random() * x); } const arr = R.map(x => { return {room: {name: Rand(x)}, structureType: Rand(x)}; }, R.repeat(30, 10000)); function GetArray() { return arr; } function SortMyStructs() { Game = {}; Game.structures = GetArray(); Game.mStructs = { }; Game.mStructs.mRooms = { }; Game.mObservers = [ ]; for (let structIter in Game.structures) { const structure = Game.structures[structIter]; const roomName = structure.room.name; const type = structure.structureType; if (undefined === Game.mStructs.mRooms[roomName]) { Game.mStructs.mRooms[roomName] = { }; Game.mStructs.mRooms[roomName].mAll = [ ]; Game.mStructs.mRooms[roomName].mType = { }; } Game.mStructs.mRooms[roomName].mAll.push(structure); if (undefined === Game.mStructs.mRooms[roomName].mType[type]) { Game.mStructs.mRooms[roomName].mType[type] = [ ]; } Game.mStructs.mRooms[roomName].mType[type].push(structure); } };
Tests:
Lodash groupBy
const tmp = _.groupBy(GetArray(), x => x.room.name); const tmp2 = _.forEach(tmp, (val, key) => tmp[key] = _.groupBy(tmp[key], x => x.structureType)); tmp2;
Ramda groupBy
R.pipe( R.groupBy(x => x.room.name), R.map(R.groupBy(x => x.structureType)) )(GetArray());
Hand written
SortMyStructs();
Using Reduce
_.reduce(GetArray(), (acc, x) => { if (!acc.mRooms) acc.mRooms = {}; if (!acc.mRooms[x.room.name]) acc.mRooms[x.room.name] = {mAll: [], mType: {}}; acc.mRooms[x.room.name].mAll.push(x); if (!acc.mRooms[x.room.name].mType[x.structureType]) acc.mRooms[x.room.name].mType[x.structureType] = []; acc.mRooms[x.room.name].mType[x.structureType].push(x); return acc; }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash groupBy
Ramda groupBy
Hand written
Using Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash groupBy
4511.0 Ops/sec
Ramda groupBy
2792.8 Ops/sec
Hand written
4413.2 Ops/sec
Using Reduce
6401.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The benchmark is designed to compare the performance of three different approaches for grouping data by two criteria: room name and structure type. The benchmark uses a JavaScript array of 30,000 objects, each containing a room and a structure type. **Library Used: Ramda** Ramda is a functional programming library that provides various utility functions for working with arrays and other data structures. In this benchmark, the `R.groupBy` function from Ramda is used to group the data by room name and structure type. The `R.map` function is also used to apply another grouping operation. **Library Used: Lodash** Lodash is a JavaScript library that provides various utility functions for working with arrays, objects, and other data structures. In this benchmark, the `_groupBy` function from Lodash is used to group the data by room name and structure type. **Hand Written Approach** The hand written approach uses a simple iterative loop to group the data by room name and structure type. **Comparison of Approaches** Here's a brief overview of each approach: 1. **Lodash `groupBy`**: This function groups an array by a specified property and returns an object with the grouped values. 2. **Ramda `groupBy`**: This function is similar to Lodash's `_groupBy`, but it uses a more functional programming style to group the data. 3. **Hand Written Approach**: This approach uses a simple iterative loop to group the data by room name and structure type. **Pros and Cons of Each Approach** Here are some pros and cons of each approach: 1. **Lodash `groupBy`**: * Pros: Easy to use, well-documented. * Cons: May not be as efficient as other approaches for large datasets. 2. **Ramda `groupBy`**: * Pros: More functional programming style, which can make the code more concise and readable. * Cons: May require more effort to understand and use, especially for those without experience with functional programming. 3. **Hand Written Approach**: * Pros: Can be more efficient than other approaches for large datasets, as it avoids the overhead of a library function call. * Cons: Requires more code and may be less readable than other approaches. **Other Considerations** When choosing an approach, consider the following factors: 1. **Performance**: If performance is critical, the hand written approach or Ramda's `groupBy` may be faster for large datasets. 2. **Readability**: If readability is important, Lodash's `_groupBy` or Ramda's `groupBy` may be a better choice. 3. **Maintainability**: If maintainability is crucial, Lodash's `_groupBy` or Ramda's `groupBy` may be easier to update and modify in the future. **Alternatives** Other approaches for grouping data by two criteria include: 1. **D3.js's `groupBy`**: A popular data visualization library that also provides a `groupBy` function. 2. **Moment.js's `groupBy`**: A date and time manipulation library that includes a `groupBy` function. 3. ** vanilla JavaScript approaches**: There are many examples of vanilla JavaScript approaches for grouping data, including using `Array.prototype.reduce()` or `Array.prototype.forEach()`.
Related benchmarks:
.find() vs direct access in object by id
lodash groupBy vs Array.reduce(3 ways) 10k
Lodash sort vs array.prototype.sort - compare with taking ids from different array
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
Lodash orderBy vs array.prototype.sort vs vanila orderBy
Comments
Confirm delete:
Do you really want to delete benchmark?