[Javascript] lodash: memoize() to improve the profermence

Link: https://lodash.com/docs#memoize

Example: 

.service('UserPresenter', function(UserConstants){
  var typeFromId = _.memoize(function(typeId){
    var obj = _.findWhere(UserConstants.types, { value: typeId});
    return obj ? obj.display : '';    
  });
  
  return {
    fullName: function(user){
      return user.firstName + ' ' + user.lastName;
    },
    type: function(user){
      return typeFromId(user.typeId);
    }
  };
})

It is useful when you want to find something like userId, then you can use memoize() to cache the result.

原文地址:https://www.cnblogs.com/Answer1215/p/5283686.html