使用带有underscore.js的memoize函数 [英] using memoize function with underscore.js

查看:96
本文介绍了使用带有underscore.js的memoize函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Underscore.js 中的 memoize 函数来缓存ajax调用的结果。我不确定我的实施情况。还有如何使用密钥检索缓存的结果数据。以下是我的实现:

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:

Javascript代码:

Javascript code:

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");

我使用memoize来存储ajax的结果是否正确。一旦将其放入缓存中,如何基于密钥进行检索。即'州'。

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 工作时,它需要一个需要被记忆为第一个参数的函数,并缓存给定参数的函数返回结果。下次如果使用相同的参数调用memoized函数,它将使用缓存的结果,并且可以避免函数的执行时间。因此减少计算时间非常重要。

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.

如上所述,上面提到的斐波纳契函数完全正常,因为参数具有原始类型。

As mentioned, the above fibonaci function it memoized works perfectly fine as the argument has a primitive type.

当您必须记住接受对象的函数时,会出现问题。要解决此问题, _.memoize 接受一个可选参数 hashFunction ,它将用于散列输入。这样,您就可以使用自己的哈希函数唯一地标识对象。

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 的默认实现(使用默认哈希函数)返回第一个参数 - 如果是JavaScript,它将返回 [Object object]

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].

所以对于例如

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

为什么默认在_.memoize中有函数是function(x){return x}

why default has function in _.memoize is function(x) {return x}

传递哈希函数可以避免这个问题

the problem can be avoided by passing a hash function

_.memoize(fn, function(input){return JSON.stringify(input)});

当我使用_.memoize作为正在处理的函数时,这对我来说是一个真正的帮助数组参数。

This was a real help for me when I was using _.memoize for a function that was working on arrays arguments.

希望这可以帮助很多人工作。

Hope this helps many people in their work.

这篇关于使用带有underscore.js的memoize函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆