javascript 在 underscore.js 中使用 memoize 功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24902019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using memoize function with underscore.js
提问by zilcuanu
I am trying to cache the result from an ajax call using memoize
function from Underscore.js
. I am not sure of my implementation. Also how to retrieve back the cached result data using the key. Below is my implementation:
我正在尝试使用memoize
函数从ajax 调用缓存结果Underscore.js
。我不确定我的实现。还有如何使用密钥取回缓存的结果数据。下面是我的实现:
Javascript code:
Javascript代码:
var cdata = $http
.get(HOST_URL + "/v1/report/states")
.success(function(data) {
//put the result in the angularJs scope object.
$scope.states = data;
});
//store the result in the cache.
var cachedResult = _.memoize(
function() {
return cdata;
}, "states");
Is my usage of memoize to store the result of ajax is correct. Also once it is put in cache, how to retrieve based on the key. i.e 'states'.
我使用 memoize 来存储 ajax 的结果是否正确。同样一旦放入缓存,如何根据密钥进行检索。即“状态”。
回答by Sanjay Bharwani
Let us understand how _.memoize
works, it takes a function which needs to be memoized as first argument and caches the result of the function return for given parameter. Next time if the memoized function is invoked with same argument it will use cached result and the execution time for the function can be avoided. So it is very important to reduce the computation time.
让我们了解_.memoize
它是如何工作的,它需要一个需要记忆的函数作为第一个参数,并缓存给定参数的函数返回结果。下次如果使用相同的参数调用记忆化的函数,它将使用缓存的结果,并且可以避免函数的执行时间。所以减少计算时间是非常重要的。
As mentioned, the above fibonaci function it memoized works perfectly fine as the argument has a primitive type.
如上所述,它记忆的上述斐波那契函数工作得非常好,因为参数具有原始类型。
The problem occurs when you have to memoize a function which accepts an object. To solve this, _.memoize
accepts an optional argument hashFunction
which will be used to hash the input. This way you can uniquely identify your objects with your own hash functions.
当您必须记住接受对象的函数时,就会出现问题。为了解决这个问题,_.memoize
接受一个可选参数hashFunction
,该参数将用于散列输入。通过这种方式,您可以使用自己的哈希函数唯一地标识您的对象。
The default implementation of _.memoize
(using the default hash function) returns the first argument as it is - in the case of JavaScript it will return [Object object]
.
_.memoize
(使用默认散列函数)的默认实现按原样返回第一个参数 - 在 JavaScript 的情况下,它将返回[Object object]
.
So for e.g.
所以对于例如
var fn = function (obj){ some computation here..}
var memoizedFn = _.memoize(fn);
memoizedFn({"id":"1"}) // we will get result, and result is cahced now
memoizedFn({"id":"2"}) // we will get cached result which is wrong
why default has function in _.memoize is function(x) {return x}
为什么默认在 _.memoize 中有函数是 function(x) {return x}
the problem can be avoided by passing a hash function
这个问题可以通过传递一个散列函数来避免
_.memoize(fn, function(input){return JSON.stringify(input)});
This was a real help for me when I was using _.memoize for a function that was working on arrays arguments.
当我将 _.memoize 用于处理数组参数的函数时,这对我很有帮助。
Hope this helps many people in their work.
希望这对很多人的工作有所帮助。
回答by sergeyz
_.memoize
takes a function:
_.memoize
接受一个函数:
var fibonacci = _.memoize(function(n) {
return n < 2 ? n: fibonacci(n - 1) + fibonacci(n - 2);
});
You should understand that this is just an extra wrapper function that makes function that you pass it as an argument smarter( Adds extra mapping object to it ).
您应该明白这只是一个额外的包装函数,它使您将其作为参数传递的函数更智能(向其添加额外的映射对象)。
In example above function that computes fibonacci number is wrapped around with _.memoize
. So on every function call (fibonacci(5)
or fibonacci(55555)
) passed argument matched to return value so if you need to call one more time fibonacci(55555)
it doesn't need to compute it again. It just fetches that value from that mapping object that _.memoize
provided internally.
在上面的例子中,计算斐波那契数的函数用_.memoize
. 因此,在每个函数调用 (fibonacci(5)
或fibonacci(55555)
) 传递的参数都与返回值匹配,因此如果您需要再调用一次fibonacci(55555)
,则不需要再次计算它。它只是从_.memoize
内部提供的映射对象中获取该值。
回答by Oscar
If you are using Angular.js's $http
, you probably just want to pass {cache : true}
as a second parameter to the get
method.
如果您使用的是 Angular.js 的$http
,您可能只想{cache : true}
作为第二个参数传递给该get
方法。
To store values using key value pairs, you may want to use $cacheFactory, as described in other answers like here. Basically:
要使用键值对存储值,你可能需要使用$ cacheFactory,像在其他的答案中描述这里。基本上:
var cache = $cacheFactory('cacheId');
cache.put('states', 'value');
cache.get('states');