Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ECMA .flat() vs Custom .flatten()
(version: 2)
Comparing performance of:
ECMA .flat() vs Custom .flatten()
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = Array(5000).fill([0, [1], 2]);
Tests:
ECMA .flat()
function flatten(list) { return list.flat(9999) } flatten(arr);
Custom .flatten()
function flatten(list, dst) { if (dst === undefined) dst = list; for (let i = 0; i < list.length; i++) { let item = list[i]; if (Array.isArray(item)) { // we need to inline it. if (dst === list) { // Our assumption that the list was already flat was wrong and // we need to clone flat since we need to write to it. dst = list.slice(0, i); } flatten(item, dst); } else if (dst !== list) { dst.push(item); } } return dst; } flatten(arr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
ECMA .flat()
Custom .flatten()
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 dive into the explanation. The provided benchmark compares two approaches to flatten a nested array: ECMA's built-in `.flat()` method and a custom implementation of `.flatten()`. **ECMA's `.flat()` Method** The ECMA's `.flat()` method is a standard JavaScript function introduced in ECMAScript 2019 (ES10). It returns an array that contains all sub-array elements as separate elements of the resulting array. The method takes two optional arguments: * `depth`: specifies the number of levels to flatten * `returnLength`: if specified, will be used for internal state and return length calculation In this benchmark, `.flat()` is used with a depth of 9999, which means it will flatten the entire array, regardless of its depth. **Custom `.flatten()` Method** The custom implementation of `.flatten()` is a manual approach to flatten an array. It takes two arguments: `list` and `dst`. The function works as follows: 1. If `dst` is undefined, it defaults to the input `list`. 2. It iterates over each element in the `list`. 3. If the element is an array, it recursively calls itself with the inner array and a cloned version of the outer array (`dst`) as the destination. 4. If the element is not an array (i.e., a primitive value), it simply appends it to the `dst` array. The custom implementation has two key differences from ECMA's `.flat()` method: * It requires manual cloning of arrays when the inner array is also an array, which can lead to increased memory usage. * It does not have any built-in optimization for deep flattening, unlike `.flat()`. **Pros and Cons** Here are some pros and cons of each approach: ECMA's `.flat()` method: Pros: * Optimized for performance and memory efficiency * Handles nested arrays recursively without manual cloning Cons: * Only available in ECMAScript 2019 (ES10) and later versions * May not be supported by older browsers or environments Custom implementation of `.flatten()`: Pros: * Portable across different JavaScript environments and versions * Easy to implement manually Cons: * Can lead to increased memory usage due to manual cloning * Lacks built-in optimizations for deep flattening **Library and Special JS Features** There is no external library used in this benchmark. However, if we were to compare the custom implementation with a third-party library like Lodash's `flatten()` function, it would be worth noting that Lodash's implementation has additional features, such as handling arrays of arbitrary depth and size. As for special JavaScript features, there are none mentioned in the provided code. If you're curious about other features or syntax, feel free to ask! **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * Lodash's `flatten()` function (mentioned above) * A recursive array flattening library like flatten-js * Using a third-party library like js-flatten Keep in mind that these alternatives may offer different trade-offs in terms of performance, memory usage, and complexity. I hope this explanation helps you understand the benchmark and the approaches being compared!
Related benchmarks:
flatMap vs map/flat
flatMap vs flat+map
Reduce Push vs. flatMap with subarrays
flat() vs flatMap()
flatMap vs flat+map 2
Comments
Confirm delete:
Do you really want to delete benchmark?