zl程序教程

您现在的位置是:首页 >  前端

当前栏目

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

JavaScript to The Improve lodash
2023-09-14 08:59:20 时间

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.