localStorage compression? - jquery

﹥>﹥吖頭↗ 提交于 2019-12-12 02:45:12

问题


I have the function which saves and clears div's style to localStorage on click:

var originalAttributes = $('.aaa').attr('style');
$('.aaa').each(function(){
    var d = $(this),
    id = d.attr('id'),
    storedStyle = window.localStorage.getItem('aaaStyle' + id);
    if (storedStyle != undefined){   //style stored
        d.attr('style', storedStyle);
    }
});

//mouse event functions for class="aaa"

$('#save').click(function () {
    $('.aaa').each(function(){
        var d = $(this),
        id = d.attr('id'),
        style = d.attr('style');
        if (style != originalAttributes){   //style changed
            //$.cookie('aaaStyle' + id, style, { expires: 30 });
            window.localStorage.setItem('aaaStyle' + id, style);
        }
    });

});

$('#clear').click(function () {
    // unset changes
    $('.aaa').attr('style',originalAttributes).each(function(){
        var d = $(this),
        id = d.attr('id');
        window.localStorage.removeItem('aaaStyle' + id);
    });
});

http://jsfiddle.net/z8KuE/33/

What should be added to this code so that data in the local storage gets compressed?

(there is memory limit per domain in each browser and I would like to maximize this functionality)


回答1:


you can compress string in webworker to not put unnecessary load on primary thread - there are libraries for strings compression, even lzma, as far as i know localStorage allows to allocate up to 20mb in modern browsers per page and 200 mb in IE shared through all sites - additional compression - you can build map of ids and classes if there is less than 255 ids and classes and save them as one character using String.fromCharCode(int) and save as one big string in localStorage then to decode - inputstr[pos].charCodeAt(0) for every pair to convert to usable object at page startup. However then it's bad idea to generate this big string on every change and i'd use onbeforeunload event to do this before page is closed



来源:https://stackoverflow.com/questions/19124425/localstorage-compression-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!