title: Refactor reselect selector for better memoization
body: <p>I'm using Redux.js and reselect library and I'm wondering how the following selector function could be refactored in order to have better memoization. The case is that even if the details of a <code>citizen</code> are updated, the total number of citizens, always remain the same. So is there a better way to take the total count only if a new item is added in the store, or an item is removed?</p>
  
  <pre class="lang-js prettyprint-override"><code>export const getCitizensNumberByContinentSelector = () =&gt; {
      let getCountryIdsByContinent = getCountryIdsByContinentSelector();
      let getCityIdsByCountry = getCityIdsByCountrySelector();
      let getCitizenIdsByCity = getCitizenIdsByCitySelector();
  
      return createSelector(
          (store) =&gt; store,
          (store, continentId) =&gt; continentId,
          (store, continentId) =&gt; {
              let citizensCount = 0;
  
              let countryIdsByContinent = getCountryIdsByContinent(store, continentId);
  
              countryIdsByContinent.forEach(countryId =&gt; {
                  let cityIdsByCountry = getCityIdsByCountry(store, countryId);
  
                  cityIdsByCountry.forEach(cityId =&gt; {
                      let citizenIdsByCity = getCitizenIdsByCity(store, cityId);
                      citizensCount += citizenIdsByCity.length;
                  });
              });
  
              return citizensCount;
          }
      );
  };
  
  function mapStateToProps(state) {
      let getCitizensNumberByContinent = getCitizensNumberByContinentSelector();
  
      return (store, ownProps) {
          return {
              citizensCount: getCitizensNumberByContinent(store, ownProps.continentId)
          };
      };
  }
  
  export default connect(mapStateToProps)(MyComponent);
  </code></pre>