HTML5 本地存储字符串化并存储每个对象引用

发布于 2024-11-06 04:29:43 字数 578 浏览 0 评论 0 原文

使用以下 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那小子欠揍 2024-11-13 04:29:43

更合适的技术是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文