使用以下 JSON:
var myObj = {name: 'my obj', does: 'nothing'};
var myObjArr = [myObj, myObj, myObj];
将 myObjArr 存储到本地存储时,myObj JSON 被写入 3 次,占用 3 倍的存储空间,即:
"[{"name":"my obj","does":"nothing"},{"name":"my obj","does":"nothing"},{"name":"my obj","does":"nothing"}]"
显然这会带来可扩展性问题。谁能推荐一个最佳解决方案?到目前为止,我不得不求助于使用 ID,即关系数据库。
var objects = {0: {name: 'my obj', does: 'nothing'}};
var myObjArr = [{obj: 0}, {obj: 0}, {obj: 0}];
更新 - 问题是当所有数据最终存储为键/值字符串时如何在本地存储中表示此层次结构。诉诸关系数据库概念似乎很老派。
With the following JSON:
var myObj = {name: 'my obj', does: 'nothing'};
var myObjArr = [myObj, myObj, myObj];
When storing myObjArr to local storage, the myObj JSON is wrtten 3 times, taking up 3 times as much storage space, i.e:
"[{"name":"my obj","does":"nothing"},{"name":"my obj","does":"nothing"},{"name":"my obj","does":"nothing"}]"
Obviously this is going to present scalability issues. Can anyone recommend an optimal solution? So far I've had to resort to using ID's, a la relational databases.
var objects = {0: {name: 'my obj', does: 'nothing'}};
var myObjArr = [{obj: 0}, {obj: 0}, {obj: 0}];
Update - the question is how to represent this hierarchy in local storage when all data is ultimately stored as key/value strings. Resorting to relational database concepts seems old-school.
发布评论
评论(1)
更合适的技术是 IndexedDB 用作对象存储,但它不是许多浏览器仍支持。
编辑:您需要浏览 结构化克隆算法 - 看起来引用是按记录维护的,但是添加多个记录不会导致每个记录引用在 JavaScript 内存空间中共享的对象。
A more appropriate technology would be IndexedDB used as an object store, however it isn't supported by many browsers yet.
EDIT: You'll want to browse through the documentation of the structured clone algorithm that is used when copying an object into IndexedDB - it looks like references are maintained per record, but adding multiple records will not result in each record referencing objects that were shared in the JavaScript memory space.