狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

有人測(cè)試了 JS 中所有循環(huán)的速度!來看看JS 中最快的循環(huán)是什么?

admin
2025年2月21日 16:10 本文熱度 1092

昨天看到有一位國外的大佬測(cè)試了 JS 中所有 【循環(huán)操作】 的性能消耗和執(zhí)行速度。

涉及到的 API 包含:for 循環(huán)、while 循環(huán)、ForEach、Map、Filter 等在內(nèi)的 十余種 API。

但是需要注意:以下測(cè)試內(nèi)容【僅供參考】。因?yàn)閷?shí)際的開發(fā)中,涉及到的場(chǎng)景要復(fù)雜的多,不能通過這種簡(jiǎn)單的測(cè)試一概而論!

讓我們來看一看吧!

涉及到的 API 一覽:For 循環(huán)、While 循環(huán)、Do-While 循環(huán)、For-Of、forEach、map、filter、reduce、some、every、find 

測(cè)試方案

在測(cè)試中,將使用 console.time() 和 console.timeEnd() 方法來測(cè)量 API 的執(zhí)行時(shí)間。偽代碼如下:

console.time ('描述信息'
// 要測(cè)量的代碼
console.timeEnd ('描述信息'

測(cè)試任務(wù)是:將 5000 萬個(gè)數(shù)據(jù)從一個(gè)數(shù)組轉(zhuǎn)移到另一個(gè)數(shù)組。 通過執(zhí)行時(shí)間,來判斷性能。

其中每個(gè)執(zhí)行都將 異步 進(jìn)行,以保證互不干擾。

執(zhí)行代碼

// 生成隨機(jī)數(shù)據(jù)
const numbersList = Array.from({ length50_000_000 }, () =>
  Math.floor(Math.random() * 100)
)

// For 循環(huán)
const usingForLoop = async (array) => {
  console.time('For 循環(huán)')

  const newNumbersList = []
  for (let i = 0; i < array.length; i++) {
    newNumbersList.push(array[i])
  }

  console.timeEnd('For 循環(huán)')
}

// While 循環(huán)
const usingWhile = async (array) => {
  console.time('WHILE')

  let i = 0
  const newNumbersList = []
  while (i < array.length) {
    newNumbersList.push(array[i])
    i++
  }

  console.timeEnd('WHILE')
}

// Do-While 循環(huán)
const usingDoWhile = async (array) => {
  console.time('DO WHILE')

  let i = 0
  const newNumbersList = []
  do {
    newNumbersList.push(array[i])
    i++
  } while (i < array.length)

  console.timeEnd('DO WHILE')
}

// For-Of 循環(huán)
const usingForOf = async (array) => {
  console.time('FOR OF')

  const newNumbersList = []
  for (const item of array) {
    newNumbersList.push(item)
  }

  console.timeEnd('FOR OF')
}

// ForEach 循環(huán)
const usingForEach = async (array) => {
  console.time('forEach')

  const newNumbersList = []
  array.forEach((item) => newNumbersList.push(item))

  console.timeEnd('forEach')
}

// Map 循環(huán)
const usingMap = async (array) => {
  console.time('map')

  const newNumbersList = array.map((number) => number)

  console.timeEnd('map')
}

// Filter 循環(huán)
const usingFilter = async (array) => {
  console.time('filter')

  const newNumbersList = array.filter((item) => true)

  console.timeEnd('filter')
}

// Reduce 循環(huán)
const usingReduce = async (array) => {
  console.time('reduce')

  const newNumbersList = array.reduce((acc, item) => {
    acc.push(item)
    return acc
  }, [])

  console.timeEnd('reduce')
}

// Some 循環(huán)
const usingSome = async (array) => {
  console.time('some')

  const newNumbersList = []
  array.some((item) => {
    newNumbersList.push(item)
    return false
  })

  console.timeEnd('some')
}

// Every 循環(huán)
const usingEvery = async (array) => {
  console.time('every')

  const newNumbersList = []
  array.every((item) => {
    newNumbersList.push(item)
    return true
  })

  console.timeEnd('every')
}

// Find 循環(huán)
const usingFind = async (array) => {
  console.time('find')

  const newNumbersList = []
  array.find((item) => {
    newNumbersList.push(item)
    return false
  })

  console.timeEnd('find')
}

// 執(zhí)行循環(huán)操作
;(async () => {
  await usingForLoop(numbersList)
  await usingWhile(numbersList)
  await usingDoWhile(numbersList)
  await usingForOf(numbersList)
  await usingForEach(numbersList)
  await usingMap(numbersList)
  await usingFilter(numbersList)
  await usingReduce(numbersList)
  await usingSome(numbersList)
  await usingEvery(numbersList)
  await usingFind(numbersList)
})()

打印結(jié)果如下:

?

【在當(dāng)前測(cè)試場(chǎng)景下】,根據(jù)測(cè)試結(jié)果所示,可發(fā)現(xiàn):

  • 原生的 For 循環(huán)依然是最快的循環(huán)方式
  • forEach 的耗時(shí)是 for 循環(huán)的 3 倍
  • For...Of 這種簡(jiǎn)化的語法,性能耗時(shí)較高,是 for 循環(huán)的 4 倍,不如 forEach
  • forEach 的循環(huán)執(zhí)行速度,竟然 不如 map


該文章在 2025/2/21 16:10:27 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲(chǔ)管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved