Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array with Objects loop
(version: 2)
Loop through an array of objects and change one property
Comparing performance of:
For vs Foreach vs For-in vs For-of vs map
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const array = Array.from({ length: 1000 }, function(_v, index) { return { index } } )
Tests:
For
for (let i = 0; i < array.length; i++) { let item = array[i] item.index = i + 1 } console.log('for', array)
Foreach
array.forEach(function(item, i) { item.index = i + 1 }); console.log('forEach', array)
For-in
for (let i in array) { let item = array[i] item.index = i + 1 } console.log('for-in', array)
For-of
let i = 0 for (let item of array) { item.index = i + 1 i++ } console.log('for-of', array)
map
array.map((item,i) => { item.index = i + 1 }) console.log('map', array)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
For
Foreach
For-in
For-of
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For
225923.7 Ops/sec
Foreach
150789.1 Ops/sec
For-in
13021.0 Ops/sec
For-of
154490.1 Ops/sec
map
116928.1 Ops/sec
Related benchmarks:
for vs foreach vs for of
for/for each/forEach1
for/for each/forEach2
for vs for...of vs Array.forEach
For of VS for VS forEach
for vs foreach vs some [with effects]
javascript rulettaa
javascript ruletz
for vs foreach vs for..in vs for..of with content
Comments
Confirm delete:
Do you really want to delete benchmark?