Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash flattenDeep vs js native
(version: 0)
Comparing performance of:
_.flattenDeep vs // Native vs Native(ES2019) vs native 2
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Script Preparation code:
var elements = [1, [2, [3, [4]], 5]]; const flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce( (a, b) => a.concat(flattenDeep(b)) , []) : [arr]; const flattenDeepL = (arr) => arr.flatMap((subArray, index) => Array.isArray(subArray) ? flattenDeepL(subArray) : subArray);
Tests:
_.flattenDeep
_.flattenDeep(elements);
// Native
const flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce( (a, b) => a.concat(flattenDeep(b)) , []) : [arr]; flattenDeep(elements)
Native(ES2019)
elements.flat(Infinity)
native 2
const flattenDeep = (arr) => arr.flatMap((subArray, index) => Array.isArray(subArray) ? flattenDeep(subArray) : subArray) flattenDeep(elements)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
_.flattenDeep
// Native
Native(ES2019)
native 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.flattenDeep
18807174.0 Ops/sec
// Native
4307239.0 Ops/sec
Native(ES2019)
13471334.0 Ops/sec
native 2
4616019.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **What is being tested?** MeasureThat.net is testing the performance of two approaches to flatten a deeply nested array in JavaScript: 1. **Lodash `flattenDeep`**: This is a library function that flattens an array recursively. 2. **Native ES2019 `flat` method**: This is a built-in JavaScript method introduced in ECMAScript 2019, which can be used to flatten arrays. **Options compared:** The test compares the performance of three different approaches: 1. **Lodash `flattenDeep`**: The original implementation provided by MeasureThat.net. 2. **Native ES2019 `flat` method**: A built-in JavaScript method that can be used to flatten arrays. 3. **Alternative implementation**: Another way of implementing `flattenDeep` using the `flatMap` method. **Pros and cons:** 1. **Lodash `flattenDeep`**: * Pros: Widely supported, well-documented, and reliable. * Cons: Adds an external dependency (Lodash), which may not be desirable for all use cases. 2. **Native ES2019 `flat` method**: * Pros: Built-in, no additional dependencies required. * Cons: Requires support for ES2019, and its performance may vary depending on the browser. 3. **Alternative implementation (native 2)**: * Pros: No external dependencies required, uses modern JavaScript features. * Cons: May have a steeper learning curve due to the use of `flatMap`, which is not as well-known as `reduce`. **Library and syntax used in test cases:** The test case using Lodash is loading the library from a CDN (`https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js`). The native ES2019 implementation uses the built-in `flat` method. There are no special JavaScript features or syntax mentioned in the provided code snippet. **Other alternatives:** If you need to flatten arrays, other alternatives to Lodash's `flattenDeep` include: 1. **Array.prototype.flat()**: This method is part of the ECMAScript 2019 standard and can be used to flatten arrays. 2. **lodash-es**: A smaller and faster version of Lodash that only includes the essential functions. 3. **A simpler implementation using recursion or iteration**: Depending on your specific requirements, you may want to implement a custom flattening function using basic JavaScript features like `for` loops or recursive functions. Keep in mind that these alternatives may have their own pros and cons, which are not discussed here for brevity.
Related benchmarks:
Lodash flatten vs nativate flat (depth 1)
Lodash Flatten vs Array.flat() with infinite
Lodash flattern vs flat
_.flatten vs .flatMap
Comments
Confirm delete:
Do you really want to delete benchmark?