原文地址:Iterating Over JavaScript Object Entries and their Performance - 5 Techniquesjavascript
本文地址:https://ssshooter.com/2020-06...java
返回對象全部可枚舉的鍵值對,不會追尋原型鏈上的 keynode
let obj = { key1: 'value1', key2: 'value2', key3: 'value3', } Object.entries(obj).forEach(entry => { let key = entry[0] let value = entry[1] // entry 會是這樣 ["key1", "value1"] })
返回對象全部可枚舉的鍵chrome
let obj = { key1: 'value1', key2: 'value2', key3: 'value3', } Object.keys(obj).forEach(key => { let value = obj[key] })
返回對象全部可枚舉的值瀏覽器
let obj = { key1: 'value1', key2: 'value2', key3: 'value3', } Object.values(obj).forEach(value => { // 只能使用 value })
迭代可枚舉屬性,會順着原型鏈找下去dom
let obj = { key1: 'value1', key2: 'value2', key3: 'value3', } for (const key in obj) { let value = obj[key] if (obj.hasOwnProperty(key)) { // 自己的 } else { // 來自原型鏈的 } }
返回對象全部(包括不可枚舉)的鍵(原文說會找原型鏈是錯的)ssh
let obj = { key1: 'value1', key2: 'value2', key3: 'value3', } Object.getOwnPropertyNames(obj).forEach(key => { let value = obj[key] })
下面的代碼用上面的幾種方法遍歷有 1000000 個屬性的對象,循環 10 次oop
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['key' + Math.random()] = 'val' + Math.random() } return obj } for (let i = 0; i < iterations; i++) { let obj = generateObject() //Object.entries performance.mark('A') Object.entries(obj).forEach(entry => { let key = entry[0] let value = entry[1] }) performance.mark('B') performance.measure('ENTRIES', 'A', 'B') //Object.Keys performance.mark('A') Object.keys(obj).forEach(key => { let value = obj[key] }) performance.mark('B') performance.measure('KEYS', 'A', 'B') //Object.Values performance.mark('A') Object.values(obj).forEach(value => {}) performance.mark('B') performance.measure('VALUES', 'A', 'B') //For In performance.mark('A') for (const key in obj) { let value = obj[key] } performance.mark('B') performance.measure('FORIN', 'A', 'B') //Object.getOwnPropertyNames performance.mark('A') Object.getOwnPropertyNames(obj).forEach(key => { let value = obj[key] }) performance.mark('B') performance.measure('GETOWP', 'A', 'B') } console.log( Object.entries(values).sort((a, b) => { return a[1] - b[1] }) )
下面的結果是我本身跑的,順序的是指賦值的時候直接用 index,隨機則是鍵值對都插入隨機數,獲得的性能排序是和做者同樣的,也由於 node.js 和 chrome 都是 V8,因此這個應該也是表明在瀏覽器上的性能排序。性能
// 順序 ;[ ['FORIN', 4677.321499], ['KEYS', 4812.776572], ['GETOWP', 8610.906197], ['VALUES', 9914.674390999999], ['ENTRIES', 19338.083694], ] // 隨機 ;[ ['KEYS', 4502.579589], ['FORIN', 4678.013548000001], ['GETOWP', 8880.325031999999], ['VALUES', 10104.106962], ['ENTRIES', 17089.637588999998], ]
以前據說引擎會猜想下一個值讓運行速度更快,看數據彷佛沒有太大影響。ui
也算是一點乾貨,快點來原文給做者鼓鼓掌吧 👏