localStorage压缩? – jquery

我有一个function,可以在点击时将div的样式保存并清除到localStorage:

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/

应该添加到此代码中以便本地存储中的数据被压缩?

(每个浏览器中的每个域都有内存限制,我想最大化此function)

你可以压缩webworker中的字符串,以免在主线程上加载不必要的负载 – 有字符串压缩的库,甚至是lzma,据我所知localStorage允许在每页现代浏览器中分配最多20mb,在IE中共享200mb sites – 附加压缩 – 如果少于255个id和类,你可以构建id和类的映射,并使用String.fromCharCode(int)将它们保存为一个字符,并在localStorage中保存为一个大字符串然后解码 – inputstr [pos ] .charCodeAt(0),用于在页面启动时转换为可用对象的每一对。 然而,在每次更改时生成这个大字符串是个坏主意,我会在页面关闭之前使用onbeforeunload事件来执行此操作