Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs spread operator vs push for array flat implementation
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method and push
Comparing performance of:
native flat vs flatPush vs flatConcat vs flatSpread
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var isArray = Array.isArray; var sourceArray = [1, [1, 2, [3, 4, [5, 6]]], 2, [3, 4, [1, 2, [3, [1, 2, [3, 4]], 4]], [5, 6, [1, 2, [3, 4, [5, 6]]], [7, 8, [9, 10]]]]]; function flatPush(arr, depth) { if (depth === undefined) { depth = 100; } else if (depth <= 0) { return arr; } var result = []; for (var len = arr.length, i = 0, val; i < len; ++i) { if (isArray((val = arr[i]))) { val = flatPush(val, depth - 1); for (var jen = val.length, j = 0; j < jen; ++j) { result.push(val[j]); } } else { result.push(val); } } return result; } function flatConcat(arr, depth) { if (depth === undefined) { depth = 100; } else if (depth <= 0) { return arr; } var result = []; for (var len = arr.length, i = 0, val; i < len; ++i) { if (isArray((val = arr[i]))) { result = result.concat(flatConcat(val, depth - 1)); } else { result.push(val); } } return result; } function flatSpread(arr, depth) { if (depth === undefined) { depth = 100; } else if (depth <= 0) { return arr; } var result = []; for (var len = arr.length, i = 0, val; i < len; ++i) { if (isArray((val = arr[i]))) { result.push(...flatSpread(val, depth - 1)); } else { result.push(val); } } return result; }
Tests:
native flat
return sourceArray.flat(100);
flatPush
return flatPush(sourceArray);
flatConcat
return flatConcat(sourceArray);
flatSpread
return flatSpread(sourceArray);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
native flat
flatPush
flatConcat
flatSpread
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!
Related benchmarks:
Array.prototype.concat vs spread operator
Array.prototype.concat vs push w/ spread operator vs spread operator
Array concat vs spread operator vs push with single element
Array concat vs spread operator vs push for single values
Comments
Confirm delete:
Do you really want to delete benchmark?