开发者

HTML5 Local Storage stringify's and stores EACH object reference

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 开发者_Go百科this hierarchy in local storage when all data is ultimately stored as key/value strings. Resorting to relational database concepts seems old-school.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜