Skip to content
Comparing performance of map method vs for in vs forEach
Swift
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Tests
.gitignore
.travis.yml
LICENSE
Package.swift
README.md

README.md

Map vs for in vs forEach Challenge

Swift 4.2.1 Build Status

map() or for in or forEach()? Comparing performance of map method vs for in vs forEach method.

Usage

Run the command below and compare the results.

$ swift test

Modify arrayLength property in MapVsForInVsForEachChallengeTests class to check performance for longer / shorter array.

Test Environment

  • 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

Test Results

Variant Average duration
map() 1.227 sec
for in 1.892 sec
forEach() 2.135 sec

Conclusions

map() is the fastest, forEach() is the slowest.

You can’t perform that action at this time.