【问题标题】:Serving from sessionStorage takes a very long time从 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 返回需要这么长时间?

【问题讨论】:

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

标签: javascript angularjs session-storage


【解决方案1】:

经过调试,问题似乎不是sessionStorage。

问题在于 AngularJS 需要花费时间来为 1000 多个元素渲染 ng-repeat。这是我接下来要研究的事情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    相关资源
    最近更新 更多