首頁 >web前端 >js教程 >JavaScript物件迭代方法和效能的比較

JavaScript物件迭代方法和效能的比較

hzc
hzc轉載
2020-06-10 16:51:463082瀏覽

Object.entries


傳回物件所有可枚舉的鍵值對,不會追尋原型鏈上的 key

let obj = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3',
}
Object.entries(obj).forEach(entry => {
  let key = entry[0]
  let value = entry[1]
  // entry 会是这样 ["key1", "value1"]
})

Object.keys


傳回物件所有可列舉的鍵

let obj = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3',
}
Object.keys(obj).forEach(key => {
  let value = obj[key]
})

Object.values


傳回物件所有可枚舉的值

let obj = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3',
}
Object.values(obj).forEach(value => {
  // 只能使用 value
})

for…in loop


迭代可列舉屬性,會順著原型鏈找下去

let obj = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3',
}
for (const key in obj) {
  let value = obj[key]
  if (obj.hasOwnProperty(key)) {
    // 本身的
  } else {
    // 来自原型链的
  }
}

Object.getOwnPropertyNames


回傳物件所有(包含不可枚舉)的鍵(原文說會找原型鏈是錯的)

let obj = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3',
}
Object.getOwnPropertyNames(obj).forEach(key => {
  let value = obj[key]
})

效能比較


下面的程式碼用上面的幾種方法遍歷有1000000 個屬性的對象,循環10 次

const { PerformanceObserver, performance } = require('perf_hooks')

let objectSize = 1000000
let iterations = 10

console.log(
  'Starting performance test with %d object size and %d iterations',
  objectSize,
  iterations
)

let values = {
  ENTRIES: 0,
  KEYS: 0,
  VALUES: 0,
  FORIN: 0,
  GETOWP: 0,
}

const obs = new PerformanceObserver(items => {
  let entry = items.getEntries()[0]
  console.log(entry.name, entry.duration)
  values[entry.name] += entry.duration
  performance.clearMarks()
})
obs.observe({ entryTypes: ['measure'] })

function generateObject() {
  let obj = {}
  for (let i = 0; i < objectSize; i++) {
    obj[&#39;key&#39; + Math.random()] = &#39;val&#39; + Math.random()
  }
  return obj
}

for (let i = 0; i < iterations; i++) {
  let obj = generateObject()

  //Object.entries
  performance.mark(&#39;A&#39;)
  Object.entries(obj).forEach(entry => {
    let key = entry[0]
    let value = entry[1]
  })
  performance.mark(&#39;B&#39;)
  performance.measure(&#39;ENTRIES&#39;, &#39;A&#39;, &#39;B&#39;)

  //Object.Keys
  performance.mark(&#39;A&#39;)
  Object.keys(obj).forEach(key => {
    let value = obj[key]
  })
  performance.mark(&#39;B&#39;)
  performance.measure(&#39;KEYS&#39;, &#39;A&#39;, &#39;B&#39;)

  //Object.Values
  performance.mark(&#39;A&#39;)
  Object.values(obj).forEach(value => {})
  performance.mark(&#39;B&#39;)
  performance.measure(&#39;VALUES&#39;, &#39;A&#39;, &#39;B&#39;)

  //For In
  performance.mark(&#39;A&#39;)
  for (const key in obj) {
    let value = obj[key]
  }
  performance.mark(&#39;B&#39;)
  performance.measure(&#39;FORIN&#39;, &#39;A&#39;, &#39;B&#39;)

  //Object.getOwnPropertyNames
  performance.mark(&#39;A&#39;)
  Object.getOwnPropertyNames(obj).forEach(key => {
    let value = obj[key]
  })
  performance.mark(&#39;B&#39;)
  performance.measure(&#39;GETOWP&#39;, &#39;A&#39;, &#39;B&#39;)
}

console.log(
  Object.entries(values).sort((a, b) => {
    return a[1] - b[1]
  })
)

下面的結果是我自己跑的,順序的是指賦值的時候直接用index,隨機則是鍵值對都插入隨機數,得到的性能排序是和作者一樣的,也因為node. js 和chrome 都是V8,所以這個應該也是代表在瀏覽器上的效能排序。

// 顺序
;[
  [&#39;FORIN&#39;, 4677.321499],
  [&#39;KEYS&#39;, 4812.776572],
  [&#39;GETOWP&#39;, 8610.906197],
  [&#39;VALUES&#39;, 9914.674390999999],
  [&#39;ENTRIES&#39;, 19338.083694],
]

// 随机
;[
  [&#39;KEYS&#39;, 4502.579589],
  [&#39;FORIN&#39;, 4678.013548000001],
  [&#39;GETOWP&#39;, 8880.325031999999],
  [&#39;VALUES&#39;, 10104.106962],
  [&#39;ENTRIES&#39;, 17089.637588999998],
]

之前聽說引擎會猜測下一個值讓運行速度更快,看數據似乎沒有太大影響。

也算是一點乾貨,快點來原文給作者鼓鼓掌吧

推薦教程:《JS教程

以上是JavaScript物件迭代方法和效能的比較的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:segmentfault.com。如有侵權,請聯絡admin@php.cn刪除