Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从Session服务存储需要很长时间_Javascript_Angularjs_Session Storage - Fatal编程技术网

Javascript 从Session服务存储需要很长时间

Javascript 从Session服务存储需要很长时间,javascript,angularjs,session-storage,Javascript,Angularjs,Session Storage,我有一个函数,它应该从sessionStorage返回字典(如果存在),如果没有,它将从API加载字典到sessionStorage,然后返回字典 self.getDictionary = function(dictionary){ var deferred = $q.defer(); var saved_dictionary = JSON.parse($window.sessionStorage.getItem('dictionary_' + dictionar

我有一个函数,它应该从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加载字典所需的时间相同


为什么从会话存储返回要花这么长时间?

调试后,问题似乎不是会话存储


问题是AngularJS需要一段时间来渲染1000+个元素的ng重复。这是下一件我必须研究的事情

为什么在调用
JSON.parse()
之前不检查
sessionStorage
中是否定义了key?我想我可以,但我认为这不会有太大的区别。据我所知,对空值调用JSON.parse()是可以忽略的。多长时间是多长时间?所花费的时间会带来什么问题?当您存储数据时,我假设它必须通过浏览器以某种方式序列化。我不希望花费那么长的时间,但是,我的第一个测试是尝试存储少量的数据,并从那里进一步得出结论/进行调试。而且,一旦检索到数据,似乎也不会将其存储在内存中。一旦它被解析,当它作为对象存储在服务中时访问它会快得多