Question: React 16.13.1

Newbie.

I add the componentDidUpdate lifecycle method to my component:

```
export default class MyComponent extends React.Component {

componentDidUpdate(prevProps, prevState) {
    console.log('componentDidUpdate');
    //this.state
}

render () { ... }

```

In an onChange handler I update the state and `componentDidUpdate` fires. However `prevState` enters as null. Also, within the method `this.state` is null.

Any ideas on what I'm doing wrong?

Answer: 
The result of `.reversed()` is just a structure that iterates backwards over the collection. It costs a constant time to create. Iterating over it costs O(n) just like any collection.

So `let rev = a.reversed()` is O(1), but `let rev = Array(a.reversed())` is O(n) since it iterates over `a` to make the copy. There is no implicit conversion. `Array` and `ReversedCollection<Array>` are completely different types. You can pass any Sequence to [`Array.init`](https://developer.apple.com/documentation/swift/array/2853698-init) and it will copy the values into a new Array.

Stdlib is open source, so you can also look at [how it's all implemented](https://github.com/apple/swift/blob/master/stdlib/public/core/Reverse.swift). You'll see that the initializer is only an assignment:

```
internal init(_base: Base) {
  self._base = _base
}

```
As you noted, Array.reversed() is an O(1) operation. It just wraps the original Array with logic that automatically adjusts the indexes as you access them for an effective reversal.

If you explicitly create a new Array from a Reversed collection, you are creating a full copy of the data and you are going to pay the O(n) cost. I don't think Swift's copy-on-write optimizations apply here.

Here is a code snippet that you could run to see the time complexity.

```
var array = [Int]()
for i in 0..<1000000 {
    array.append(i)
}

let startDate = Date()
let reversed = array.reversed() // O(1)
print(Date().timeIntervalSince(startDate)) // ~0s from start

var newArray = Array(reversed) // O(n)
print(Date().timeIntervalSince(startDate)) // ~0.2s from start

newArray[0] = 0
print(Date().timeIntervalSince(startDate)) // ~0.2s from start

```