Nicholas Porter
1 min readJan 22, 2020

--

My previous comment pertained to arrays only, not objects (which your article it about), so I did some testing and it seems in terms of performance the for-in and the for-of are quite similar. Here we create an object of 10 million entries and do the calculations.

let obj = {}for(let i =0; i< 10000000; i++){
obj[i] = i
}

function forInPerformance(x){
let t = Date.now();
for(y in x){y}
let t2 = Date.now()
console.log("for in: " , t2-t)
}
forInPerformance(obj)
// For In Timing: 2897 ms
function forOfPerformance(x){
let t = Date.now();
for (let prop of Object.keys(obj)) {}
let t2 = Date.now()
console.log("for in: " , t2-t)
}
forOfPerformance(obj)
// For Of Timing: 2890 ms

For 10 million iterations on the obj they both take ~2800 ms

--

--

Nicholas Porter
Nicholas Porter

No responses yet