Skip to content

Instantly share code, notes, and snippets.

@Connormiha
Created July 18, 2024 14:13
Show Gist options
  • Save Connormiha/feed0d799a97152cac8030b119f7b85e to your computer and use it in GitHub Desktop.
Save Connormiha/feed0d799a97152cac8030b119f7b85e to your computer and use it in GitHub Desktop.
spread vs unshift
// spread.js
let a = [];
console.time('1');
for (let i = 0; i < 100; i++) {
const res = [];
for (let j = 0;j<5;j++) {
res.push(j);
}
a = [...res, ...a];
}
console.timeEnd('1');
console.log(a[100]);
//unshift.js
let a = [];
console.time('1');
for (let i = 0; i < 100; i++) {
const res = [];
for (let j = 0;j<5;j++) {
res.push(j);
}
a.unshift(...res);
}
console.timeEnd('1');
console.log(a[100]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment