从sessionStorage提供服务需要很长时间

最后发布: 2017-04-13 16:10:26


问题

我有一个应该从sessionStorage返回字典的函数(如果存在),如果不存在,它将从API将字典加载到sessionStorage中,然后返回字典。

self.getDictionary = function(dictionary){
        var deferred = $q.defer();
        var saved_dictionary = JSON.parse($window.sessionStorage.getItem('dictionary_' + dictionary));

        if (saved_dictionary && saved_dictionary !== "null"){
            deferred.resolve(saved_dictionary);
        } else {
            var apiData = {module: "Dictionaries", method: "getDictionary", data: {name: dictionary}};
            apiService.execute(apiData).then(function (response) {
                $window.sessionStorage.setItem('dictionary_' + dictionary, JSON.stringify(response));
                deferred.resolve(response);
            });
        }
        return deferred.promise;
    };

我正在尝试缓存字典,因为它可能非常大(约0.5mb JSON)。

范例字典:

{"DictionaryName":"Gaming","DictionaryCategory":[{"CategoryName":"Games","MandatoryAtLeast":1,"CategoryWords":[{"title":"zelda","score":1,"exact":true,"mandatory":false,"reject":false},{"title":"mass effect","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"pokemon","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"fallout","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"cs:go","score":1,"exact":true,"mandatory":false,"reject":false},{"title":"sims","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"until dawn","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"deus ex","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"skyrim","score":1,"exact":false,"mandatory":true,"reject":false}]},{"CategoryName":"Companies","MandatoryAtLeast":1,"CategoryWords":[{"title":"bioware","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"bethesda","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"steam","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"valve","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"alienware","score":1,"exact":false,"mandatory":false,"reject":false}]},{"CategoryName":"other","MandatoryAtLeast":1,"CategoryWords":[{"title":"gamer","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"mods","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"horror","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"survival","score":1,"exact":true,"mandatory":false,"reject":false},{"title":"multiplayer","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"action","score":1,"exact":true,"mandatory":true,"reject":false},{"title":"fps","score":1,"exact":false,"mandatory":true,"reject":false},{"title":"shooter","score":1,"exact":false,"mandatory":false,"reject":false},{"title":"mmo","score":1,"exact":false,"mandatory":true,"reject":false}]}]}

问题在于从缓存中提供字典服务无济于事。 从sessionStorage提供字典所需的时间几乎与从远程API加载字典所需的时间相同。

从sessionStorage返回为什么要花这么长时间?

javascript angularjs session-storage
回答

调试后,似乎问题出在不是sessionStorage。

问题是AngularJS花费了很多时间来渲染1000多个元素的ng-repeat。 那是我接下来要研究的下一件事