I want to store JSON data in local storage. Sometimes data stored could be more than 5MB(max threshold allowed by browsers for each domain). Is there anyway i can compress or zip the data and store it in local storage ? How much latency it will add if compression and decompression for every JS function on large data ?
我想将JSON数据存储在本地存储中。有时存储的数据可能超过5MB(浏览器允许每个域的最大阈值)。无论如何我可以压缩或压缩数据并将其存储在本地存储中吗?如果对大型数据上的每个JS函数进行压缩和解压缩,它会增加多少延迟?
I am using this json data to display on webpage..... It is like a static data which is mostly same for entire session. we provide actions like search, filter etc on this json data...Whenever user enters a keyword, i search in this json data which is stored in local storage instead of making call to server
我正在使用这个json数据在网页上显示.....它就像一个静态数据,对于整个会话来说大致相同。我们在这个json数据上提供搜索,过滤等操作...每当用户输入关键字时,我会搜索存储在本地存储中的这个json数据,而不是调用服务器
1 个解决方案
#1
10
As localStorage functionality seems to be limited to handle only string key/value pairs what you can do is to use lz-string library to stringify the json object and compress it before storing it in localStorage
由于localStorage功能似乎仅限于处理字符串键/值对,您可以做的是使用lz-string库对json对象进行字符串化并压缩它,然后将其存储在localStorage中
Usage
用法
// sample json object
var jsonobj = {'sample': 'This is supposed to be ling string', 'score': 'another long string which is going to be compressed'}
// compress string before storing in localStorage
localStorage.setItem('mystring', LZString.compress(JSON.stringify(jsonobj)));
// decompress localStorage item stored
var string = LZString.decompress(localStorage.getItem('mystring'))
// parse it to JSON object
JSON.parse(string);
Check jsfiddle http://jsfiddle.net/raunakkathuria/7PQtC/1/ as i have copied the complete script so look for running code at end of fiddle
检查jsfiddle http://jsfiddle.net/raunakkathuria/7PQtC/1/,因为我复制了完整的脚本,所以在小提琴的末尾寻找运行代码
#1
10
As localStorage functionality seems to be limited to handle only string key/value pairs what you can do is to use lz-string library to stringify the json object and compress it before storing it in localStorage
由于localStorage功能似乎仅限于处理字符串键/值对,您可以做的是使用lz-string库对json对象进行字符串化并压缩它,然后将其存储在localStorage中
Usage
用法
// sample json object
var jsonobj = {'sample': 'This is supposed to be ling string', 'score': 'another long string which is going to be compressed'}
// compress string before storing in localStorage
localStorage.setItem('mystring', LZString.compress(JSON.stringify(jsonobj)));
// decompress localStorage item stored
var string = LZString.decompress(localStorage.getItem('mystring'))
// parse it to JSON object
JSON.parse(string);
Check jsfiddle http://jsfiddle.net/raunakkathuria/7PQtC/1/ as i have copied the complete script so look for running code at end of fiddle
检查jsfiddle http://jsfiddle.net/raunakkathuria/7PQtC/1/,因为我复制了完整的脚本,所以在小提琴的末尾寻找运行代码