for-in vs Object.keys forEach without inherited properties

后端 未结 6 1941
萌比男神i
萌比男神i 2020-12-15 03:19

I was looking at a perf benchmark of Object.keys + forEach vs for-in with normal objects.

This benchmark shows that Obj

6条回答
  •  眼角桃花
    2020-12-15 04:04

    And for the ES6 fans out there, looks like

    Object.keys(obj).reduce((a,k) => {a += obj[k]; return a}, res)
    

    is by far the fastest.

    https://jsperf.com/for-in-vs-for-of-keys-vs-keys-reduce

提交回复
热议问题