function copyUsingArrayDestructuring(arr) {
return [...arr]
}

function copyArrayInForOfLoop(array) {
const clone = []
for (const item of array) {
clone.push(item)
}
return clone
}

function copyArrayUsingMap(arr) {
return arr.map((x) => x)
}

function copyArrayOneByOne(arr) {
const clone = []
for (let i = 0; i <= arr.length; i++) {
clone[i] = arr[i]
}
return clone
}

function copyArrayFromTheEnd(arr) {
const clone = []
for (let i = arr.length - 1; i >= 0; i--) {
clone[i] = arr[i]
}
return clone
}

function copyUsingIteratingOverProperties(obj) {
const clone = {}
for (const property in obj) {
clone[property] = obj[property]
}
return clone
}

function copyUsingObjectAssign(obj) {
return Object.assign({}, obj)
}

function copyUsingObjectDestructuring(obj) {
return { ...obj }
}

function copyUsingJson(arr) {
return JSON.parse(JSON.stringify(arr))
}

const millionItems = Array.from(Array(1000000), (_, i) => i)

function runMillionOnce(fn) {
const start = performance.now()
fn(millionItems)
return performance.now() - start
}

const thousandItems = Array.from(Array(1000), (_, i) => i)

async function runThousandThousandTimesSync(fn) {
const start = performance.now()
for (let j = 1; j <= 1000; j++) {
fn(thousandItems)
}
return performance.now() - start
}

async function runThousandThousandTimesAsync(fn) {
const start = performance.now()
const promises = []
for (let j = 1; j <= 1000; j++) {
promises.push(() => fn(thousandItems))
}
await Promise.all(promises)
return performance.now() - start
}

async function run(title, runFn, functions) {
console.log()
console.log(title)
for (const fn of functions) {
for (let i = 1; i <= 3; i++) {
const time = await runFn(fn)
console.log(`${fn.name} ${i}: ${Math.round(time)}ms`)
}
}
}

async function main() {
const functions = [
copyUsingArrayDestructuring,
copyArrayOneByOne,
copyArrayUsingMap,
copyArrayInForOfLoop,
copyArrayFromTheEnd,
copyUsingJson,
copyUsingIteratingOverProperties,
copyUsingObjectAssign,
copyUsingObjectDestructuring,
]
await run("Million once", runMillionOnce, functions)
await run(
"Thousand thousand times sync",
runThousandThousandTimesSync,
functions,
)
await run(
"Thousand thousand times async",
runThousandThousandTimesAsync,
functions,
)
}
main()
x
const millionItems = Array.from(Array(1000000), (_, i) => i)
 
function copyUsingArrayDestructuring(arr) {
  return [...arr]
}
function copyArrayInForOfLoop(array) {
  const clone = []
  for (const item of array) {
    clone.push(item)
  }
  return clone
}
"Million once" Promise {}"copyUsingArrayDestructuring 1: 3ms" "copyUsingArrayDestructuring 2: 2ms" "copyUsingArrayDestructuring 3: 2ms" "copyArrayOneByOne 1: 11ms" "copyArrayOneByOne 2: 13ms" "copyArrayOneByOne 3: 12ms" "copyArrayUsingMap 1: 13ms" "copyArrayUsingMap 2: 13ms" "copyArrayUsingMap 3: 11ms" "copyArrayInForOfLoop 1: 22ms" "copyArrayInForOfLoop 2: 19ms" "copyArrayInForOfLoop 3: 18ms" "copyArrayFromTheEnd 1: 33ms" "copyArrayFromTheEnd 2: 35ms" "copyArrayFromTheEnd 3: 40ms" "copyUsingJson 1: 42ms" "copyUsingJson 2: 42ms" "copyUsingJson 3: 36ms" "copyUsingIteratingOverProperties 1: 84ms" "copyUsingIteratingOverProperties 2: 102ms" "copyUsingIteratingOverProperties 3: 73ms" "copyUsingObjectAssign 1: 233ms" "copyUsingObjectAssign 2: 257ms" "copyUsingObjectAssign 3: 252ms" "copyUsingObjectDestructuring 1: 202ms" "copyUsingObjectDestructuring 2: 236ms" "copyUsingObjectDestructuring 3: 299ms" "Thousand thousand times sync" "copyUsingArrayDestructuring 1: 22ms" "copyUsingArrayDestructuring 2: 0ms" "copyUsingArrayDestructuring 3: 0ms" "copyArrayOneByOne 1: 4ms" "copyArrayOneByOne 2: 3ms" "copyArrayOneByOne 3: 3ms" "copyArrayUsingMap 1: 11ms" "copyArrayUsingMap 2: 11ms" "copyArrayUsingMap 3: 12ms" "copyArrayInForOfLoop 1: 7ms" "copyArrayInForOfLoop 2: 4ms" "copyArrayInForOfLoop 3: 4ms" "copyArrayFromTheEnd 1: 12ms" "copyArrayFromTheEnd 2: 9ms" "copyArrayFromTheEnd 3: 11ms" "copyUsingJson 1: 23ms" "copyUsingJson 2: 22ms" "copyUsingJson 3: 23ms" "copyUsingIteratingOverProperties 1: 36ms" "copyUsingIteratingOverProperties 2: 23ms" "copyUsingIteratingOverProperties 3: 28ms" "copyUsingObjectAssign 1: 114ms" "copyUsingObjectAssign 2: 111ms" "copyUsingObjectAssign 3: 132ms" "copyUsingObjectDestructuring 1: 135ms" "copyUsingObjectDestructuring 2: 134ms" "copyUsingObjectDestructuring 3: 150ms" "Thousand thousand times async" "copyUsingArrayDestructuring 1: 0ms" "copyUsingArrayDestructuring 2: 0ms" "copyUsingArrayDestructuring 3: 0ms" "copyArrayOneByOne 1: 0ms" "copyArrayOneByOne 2: 0ms" "copyArrayOneByOne 3: 0ms" "copyArrayUsingMap 1: 0ms" "copyArrayUsingMap 2: 0ms" "copyArrayUsingMap 3: 0ms" "copyArrayInForOfLoop 1: 0ms" "copyArrayInForOfLoop 2: 0ms" "copyArrayInForOfLoop 3: 0ms" "copyArrayFromTheEnd 1: 0ms" "copyArrayFromTheEnd 2: 0ms" "copyArrayFromTheEnd 3: 0ms" "copyUsingJson 1: 0ms" "copyUsingJson 2: 0ms" "copyUsingJson 3: 0ms" "copyUsingIteratingOverProperties 1: 0ms" "copyUsingIteratingOverProperties 2: 0ms" "copyUsingIteratingOverProperties 3: 0ms" "copyUsingObjectAssign 1: 0ms" "copyUsingObjectAssign 2: 0ms" "copyUsingObjectAssign 3: 0ms" "copyUsingObjectDestructuring 1: 0ms" "copyUsingObjectDestructuring 2: 0ms" "copyUsingObjectDestructuring 3: 0ms"