Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash vs Native reduce + Object.keys for collections
(version: 0)
Comparing performance of:
Lodash vs Native + pickBy
Created:
4 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 = {}; for (var i = 0, len = 1000; 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(obj, (acc, val, key) => { if(val) { acc.push(key); } return acc; }, [])
Native + pickBy
Object.keys(_.pickBy(obj, (item) => !!item));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Native + pickBy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
1486533.8 Ops/sec
Native + pickBy
451152.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **What is being tested?** The provided JSON represents two test cases for measuring the performance of JavaScript microbenchmarks. The benchmarks are designed to compare the execution times of two approaches: 1. Using Lodash's `reduce` function with a custom callback. 2. Using the native `Object.keys` and `pickBy` functions. **Options being compared** In the first test case, "Lodash", the benchmark is measuring the performance of the following code: ```javascript _.reduce(obj, (acc, val, key) => { if(val) { acc.push(key); } return acc; }, []); ``` The options being compared are: * Using Lodash's `reduce` function with a custom callback. * The native JavaScript `Array.prototype.reduce()` method. **Pros and cons of each approach** Using Lodash's `reduce` function has the following pros and cons: Pros: * Provides a concise and expressive way to perform reductions on arrays. * Handles edge cases and provides a consistent API. Cons: * Requires an additional library dependency (Lodash). * May introduce overhead due to the need to load and parse the library. Using the native `Array.prototype.reduce()` method has the following pros and cons: Pros: * Is built-in to JavaScript, eliminating the need for an external library. * Does not introduce any additional dependencies or overhead. Cons: * Requires a more verbose API than Lodash's `reduce` function. * May have slightly different behavior for certain edge cases. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions and methods for tasks such as array manipulation, object creation, and string formatting. The `reduce` function in particular is designed to perform accumulations on arrays, reducing the resulting value to a single output value. In this benchmark, Lodash's `reduce` function is used to iterate over an array of objects and extract the keys from each object. The custom callback function takes three arguments: `acc`, `val`, and `key`. It pushes the key into the accumulator array only if the value is truthy, and returns the updated accumulator array. **Special JavaScript feature or syntax** In this benchmark, no special JavaScript features or syntax are being used. However, it's worth noting that Lodash's `reduce` function relies on a few implicit assumptions about the behavior of JavaScript arrays, such as: * That iteration over an array results in each element being processed sequentially. * That the `acc` parameter is initialized to an empty value (in this case, an empty array). These assumptions are implicit in JavaScript and are not explicitly specified by the language standard. **Other alternatives** If you're looking for alternative libraries or implementations of the reduction algorithm, some options include: * Underscore.js: Another popular utility library that provides a `reduce` function with similar behavior to Lodash's version. * Ramda: A functional programming library that provides a `reduce` function as part of its core API. * Google Closure Library: A collection of reusable JavaScript libraries and tools, including an implementation of the reduction algorithm. Note that these alternatives may have different performance characteristics or trade-offs in terms of complexity and expressiveness.
Related benchmarks:
Lodash vs Native reduce on object - updated 2
Iterating Lodash Entries vs Object.entries (10,000 entries)
Lodash reduce vs transform vs Native reduce on object
Lodash reduce vs transform vs Native reduce on object (Object without prototype)
Comments
Confirm delete:
Do you really want to delete benchmark?