Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash reduce vs transform vs Native reduce on object (Object without prototype)
(version: 0)
Comparing performance of:
Lodash reduce vs Native vs Lodash transform
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Script Preparation code:
window.obj = Object.create(null); for (var i = 0, len = 10; i < len; i++) { var arr = [] obj['key' + i] = arr for (var n = 0, len = 10; n < len; n++) { arr.push({ ['key' + n]: n }); } }
Tests:
Lodash reduce
_.reduce(obj, (acc, val, key) => { acc[key] = val.map(d => ({ label: key, value: val })); return acc; }, Object.create(null))
Native
Object.entries(obj).reduce((acc, [key, val]) => { acc[key] = val.map(d => ({ label: key, value: val })); return acc; }, Object.create(null))
Lodash transform
_.transform(obj, (acc, val, key) => { acc[key] = val.map(d => ({ label: key, value: val })); }, Object.create(null))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash reduce
Native
Lodash transform
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
14 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash reduce
1646361.4 Ops/sec
Native
881463.2 Ops/sec
Lodash transform
710981.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing three approaches to reduce an object in JavaScript: 1. **Lodash `reduce`**: This uses the Lodash library, which provides a utility function for functional programming tasks. 2. **Native `Object.entries()` + `reduce`**: This uses the built-in `Object.entries()` method and the native `Array.prototype.reduce()` method to achieve the same result without an external library. 3. **Lodash `transform`**: Similar to `reduce`, but uses a different utility function provided by Lodash. **Options Compared** The three approaches are being compared in terms of: * Performance (measured by executions per second) * Code complexity and readability **Pros and Cons of Each Approach** 1. **Lodash `reduce`**: * Pros: concise, readable code; well-tested and optimized by the Lodash team * Cons: relies on an external library, which may not be available in all environments 2. **Native `Object.entries()` + `reduce`**: * Pros: no external dependencies, efficient use of native methods * Cons: requires more code to achieve the same result; less readable than Lodash's implementation 3. **Lodash `transform`**: * Pros: similar to `reduce`, but might be faster due to optimized implementation * Cons: not as widely used or well-tested as `reduce` **Library Usage** The benchmark uses the following libraries: 1. **Lodash**: A popular utility library for functional programming tasks, providing functions like `reduce` and `transform`. 2. **YaBrowser/24.1.0.0 Safari/537.36** (not a library, but the browser being used): This is the browser being tested, which might have specific optimizations or behaviors that affect the benchmark results. **Special JavaScript Feature or Syntax** There are no special JavaScript features or syntaxes mentioned in this benchmark. The code uses standard JavaScript and Lodash functions. **Other Alternatives** If you're interested in exploring alternative approaches to reduce an object in JavaScript, consider: 1. **Using a different library**: Other utility libraries like Underscore.js, Ramda, or Mochas might provide similar functions for reducing objects. 2. **Implementing a custom `reduce` function**: You could write your own implementation of the `reduce` function using standard JavaScript methods. 3. **Using a different approach**: Instead of reducing an object, consider using other data structures like arrays or maps to achieve the desired result. Keep in mind that performance and readability are crucial when choosing an approach for reducing objects in JavaScript. The Lodash implementations provide concise and readable code, while the native implementation offers no external dependencies but requires more code.
Related benchmarks:
Lodash vs Native reduce on object - updated 2
Lodash vs Native reduce on object x100
Lodash vs Native reduce + Object.keys for collections
Lodash reduce vs transform vs Native reduce on object
Comments
Confirm delete:
Do you really want to delete benchmark?