# General remarks 

Here are some general remarks that could make your code look a little cleaner :

 - Avoid using `;` at the end of an instruction unless you have to put multiple instructions in the same line.
 - Document the functions properly: The description of the `complition` parameter of `splitToFitSize` isn't well formatted.
 - The correct spelling of `complition` is `completion`.
 - This is subjective and probably nitpicky: Put a space only where needed, one after the closing curly brace of a scope: `} else `, and none before a punctuation sign:  `(size: size, font: font)`.


# Catching the culprits

[![Dalton Cousins][1]][1]

Sources of slow code:

 - `" \(content)"`: String interpolation is slow as previously stated [here](https://codereview.stackexchange.com/a/213879/49921).
 - `self.components(separatedBy: .whitespaces)`: This has both a time and space [complexity](https://developer.apple.com/documentation/foundation/nsstring/1413214-components) of O(*n*).
 - `DispatchQueue.global(qos: .background)` : This is the [lowest priority](https://developer.apple.com/documentation/dispatch/dispatchqos/qosclass/background) you could give to code and it wastes time by .
 - `contents.removeFirst()`: This is called in multiple places and repeatedly. Each call is O(*n*) since all the elements of the `contents` array shifted (Have a look [here](https://developer.apple.com/documentation/swift/array/2886730-removefirst)). This means that this algotithm shifts the elements of the `contents` array n + (n-1) + (n-2) + ... + 1 times. Knowing that :
 $$n + (n-1) + (n-2) + ... + 1 = \frac{n(n+1)}{2}$$

  it makes this algorithm O(*n<sup>2</sup>*), with *n* being the number of elements in `contents`. To circumvent this use an index that traverses the array.




  [1]: https://i.sstatic.net/BlSMr.jpg
  [2]: https://github.com/nishanthooda/FitMyLabel