I've traced this through carefully and I think the better place to put it is up a few lines right when the NaN was created:

```
if s in terms:
    terms[s] += c
    if terms[s] is S.NaN: return ...
```

That way we only do the test when we *might* have created a NaN, not for every item in terms. 

Consider these two scenarios: if we add two expressions together and every term in one has a matching term in the other then we will have checked for NaN once for each term if the current PR is committed. But if there are fewer matching terms then there are two benefits to making the change where I propose: 1) we check for the NaN fewer times, only when there are terms being combined and 2) we have a quicker exit by leaving before all terms are processed.