Skip to content

Instantly share code, notes, and snippets.

@piglovesyou
Created May 6, 2022 06:58
Show Gist options
  • Save piglovesyou/25227aafe37c1f136f746e9803c9e3eb to your computer and use it in GitHub Desktop.
Save piglovesyou/25227aafe37c1f136f746e9803c9e3eb to your computer and use it in GitHub Desktop.
JavaScript speed comparison: [...array] vs. array.concat()
const a = Array.from(Array(1000_000)).map((_, i) => i);
const b = Array.from(Array(1000_000)).map((_, i) => i);
const times = 1000;
console.time("spread");
for (let i = 0; i < times; i++) {
const c = [...a, ...b];
c.length;
}
console.timeEnd("spread");
console.time("concat");
for (let i = 0; i < times; i++) {
const d = a.concat(b);
d.length;
}
console.timeEnd("concat");
@piglovesyou
Copy link
Author

Node v17.8.0

node a.js
spread: 11.645s
concat: 4.486s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment