Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Array flatten implementations [high volume]
Compare various ways to flatten nested arrays 1 level deep.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser:
Firefox 146
Operating system:
Windows
Device Platform:
Desktop
Date tested:
5 months ago
Test name
Executions per second
reduce + concat
193.7 Ops/sec
flat
1742.6 Ops/sec
Manual copy by index #1
2351.8 Ops/sec
Manual copy by index #2
2923.5 Ops/sec
Manual copy with push
2726.7 Ops/sec
reduce + spread
35.9 Ops/sec
Script Preparation code:
var params = []; for (let i = 0; i < 100; i++) { const arr = []; for (let j = 0; j < 1000; j++) { arr.push(999 * Math.random()); } params.push(arr); } function getTotal(arr) { let total = 0; for (let i = 0; i < arr.length; i++) { total += arr[i].length; } return total; }
Tests:
reduce + concat
return params.reduce((acc, val) => acc.concat(val), []);
flat
return params.flat();
Manual copy by index #1
const result = new Array(getTotal(params)); for (let i = 0; i < params.length; i++) { for (let j = 0; j < params[i].length; j++) { result[params[i].length * i + j] = params[i][j]; } } return result;
Manual copy by index #2
const result = new Array(getTotal(params)); for (let i = 0, k = 0; i < params.length; i++) { const arr = params[i]; for (let j = 0; j < arr.length; j++) { result[k] = arr[j]; k++; } } return result;
Manual copy with push
const result = []; for (let i = 0; i < params.length; i++) { const arr = params[i]; for (let j = 0; j < arr.length; j++) { result.push(arr[j]); } } return result;
reduce + spread
return params.reduce((acc, val) => [...acc, ...val], []);