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: 4ms" "copyUsingArrayDestructuring 2: 2ms" "copyUsingArrayDestructuring 3: 3ms" "copyArrayOneByOne 1: 14ms" "copyArrayOneByOne 2: 16ms" "copyArrayOneByOne 3: 13ms" "copyArrayUsingMap 1: 14ms" "copyArrayUsingMap 2: 15ms" "copyArrayUsingMap 3: 12ms" "copyArrayInForOfLoop 1: 26ms" "copyArrayInForOfLoop 2: 26ms" "copyArrayInForOfLoop 3: 26ms" "copyArrayFromTheEnd 1: 50ms" "copyArrayFromTheEnd 2: 46ms" "copyArrayFromTheEnd 3: 49ms" "copyUsingJson 1: 43ms" "copyUsingJson 2: 41ms" "copyUsingJson 3: 32ms" "copyUsingIteratingOverProperties 1: 112ms" "copyUsingIteratingOverProperties 2: 79ms" "copyUsingIteratingOverProperties 3: 77ms" "copyUsingObjectAssign 1: 270ms" "copyUsingObjectAssign 2: 336ms" "copyUsingObjectAssign 3: 314ms" "copyUsingObjectDestructuring 1: 290ms" "copyUsingObjectDestructuring 2: 279ms" "copyUsingObjectDestructuring 3: 222ms" "Thousand thousand times sync" "copyUsingArrayDestructuring 1: 1ms" "copyUsingArrayDestructuring 2: 0ms" "copyUsingArrayDestructuring 3: 0ms" "copyArrayOneByOne 1: 7ms" "copyArrayOneByOne 2: 7ms" "copyArrayOneByOne 3: 3ms" "copyArrayUsingMap 1: 12ms" "copyArrayUsingMap 2: 14ms" "copyArrayUsingMap 3: 14ms" "copyArrayInForOfLoop 1: 7ms" "copyArrayInForOfLoop 2: 4ms" "copyArrayInForOfLoop 3: 4ms" "copyArrayFromTheEnd 1: 13ms" "copyArrayFromTheEnd 2: 7ms" "copyArrayFromTheEnd 3: 8ms" "copyUsingJson 1: 19ms" "copyUsingJson 2: 14ms" "copyUsingJson 3: 14ms" "copyUsingIteratingOverProperties 1: 23ms" "copyUsingIteratingOverProperties 2: 22ms" "copyUsingIteratingOverProperties 3: 23ms" "copyUsingObjectAssign 1: 97ms" "copyUsingObjectAssign 2: 137ms" "copyUsingObjectAssign 3: 111ms" "copyUsingObjectDestructuring 1: 120ms" "copyUsingObjectDestructuring 2: 106ms" "copyUsingObjectDestructuring 3: 101ms" "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"