Run details:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Chrome 109
Linux
Desktop
2 years ago
Test name Executions per second
Custom Flatten Function 849704.4 Ops/sec
Array.prototype.flat 1040658.2 Ops/sec
Script Preparation code:
x
 
function flatten(arr) {
    let i = 0;
    const len = arr.length;
    const result = [];
    let item;
    while (i < len) {
        item = Array.isArray(arr[i]) ? flatten(arr[i]) : [arr[i]];
        [].push.apply(result, item);
        i += 1;
    }
    return result;
}
Tests:
  • Custom Flatten Function

     
    var params = [ "hello", true, 7 ];
    var other = flatten([1, 2, params]);
  • Array.prototype.flat

     
    var params = [ "hello", true, 7 ];
    var other = [1, 2, params].flat()