map()
or for in
or forEach()
? Comparing performance of map method vs for in vs forEach method.
Run the command below and compare the results.
$ swift test
Modify
arrayLength
property inMapVsForInVsForEachChallengeTests
class to check performance for longer / shorter array.
- Device: MacBook Pro (15-inch, Mid 2015, 2.2GHz Intel Core i7, 16GB RAM)
- OS: macOS High Sierra 10.13.4
- Swift: 4.1
- Array length: 10,000,000
Variant | Average duration |
---|---|
map() |
1.227 sec |
for in |
1.892 sec |
forEach() |
2.135 sec |
map()
is the fastest, forEach()
is the slowest.