Does Object.keys traverse objects?

Object.keys(obj).forEach(item => {
    console.log(obj[item])
})

is this equivalent to traversing twice, and will it have an impact on performance?

Mar.09,2021

is traversed twice. But I don't understand the intention of your code. The time complexity is that the O (n)
Object.keys () method returns an array of enumerable attributes of a given object
forEach () method to execute the provided function once for each element of the array.
your code

Object.keys(obj).forEach(item => {
    console.log(obj[item])
})
The

Object.keys (obj) traversal returns the key array of obj, but the forEach outputs the value value

of obj .

maybe you want to traverse the key value of the output object

var obj = {
    '0': 'a',
    '1': 'b',
    '2': 'c'
}
console.log(...(Object.keys(obj))) 
// 0 1 2

according to polyfill , it has to be traversed twice. However, if the order of magnitude is very small and the number of calls is not frequent, there is not much impact on performance


your code just for in once traversing.

Menu