indexedDB 在概念上与 HTML5 本地存储有何不同?

发布于 2024-11-05 13:00:40 字数 232 浏览 2 评论 0原文

  1. indexedDB 和本地存储都是键值存储。什么是
    拥有两个键/值存储的优势?
  2. indexedDB是异步的,但是join(最耗时的事情)
    是手动的。它们似乎与异步调用在同一线程中运行
    被制造了。这如何不阻塞 UI?
  3. indexedDB 允许更大的存储。为什么不增加尺寸
    HTML5 商店?
  4. 我正在挠头。 indexedDB有什么意义?
  1. Both indexedDB and local storage are key value stores. What is the
    advantage of having two key/value stores?
  2. indexedDB is asynchronous, but joins (the most time-consuming thing)
    are manual. They appear to run in the same thread as the async calls
    were made. How will this not block the UI?
  3. indexedDB allows a larger store. Why not increase the size of the
    HTML5 store?
  4. I'm scratching my head. What is the point of indexedDB?

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

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

发布评论

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

评论(4

情话已封尘 2024-11-12 13:00:40

IndexedDB 与本地存储不同,它不是键值存储。本地存储只存储字符串,因此要将对象放入本地存储,通常的方法是 JSON.stringify it:

myObject = {a: 1, b: 2, c: 3};
localStorage.setItem("uniq", JSON.stringify(myObject));

这对于查找带有键 uniq 的对象来说很好,但是从本地存储中获取 myObject 的属性的唯一方法是 JSON.parse 对象并检查它:

var myStorageObject = JSON.parse(localStorage.getItem("uniq"));
window.alert(myStorageObject.b);

这很好如果你只有本地存储中的一个或几个对象。但是想象一下,您有一千个对象,所有这些对象都有一个属性 b,并且您想对那些 b==2 的对象执行某些操作。使用本地存储,您必须循环遍历整个商店并检查每个项目的 b,这是大量浪费的处理。

使用 IndexedDB,您可以存储值中字符串以外的内容:“这包括简单类型,例如 DOMString 和 Date 以及 Object 和 Array 实例。”不仅如此,您还可以在您要查找的对象的属性上创建索引存储在值中。因此,使用 IndexedDb,您可以将同样的一千个对象放入其中,但在 b 属性上创建索引,并使用它来检索 b==2 处的对象,而无需额外开销扫描商店中的每件物品。

至少是这样的。 IndexedDB API 不是很直观。

它们似乎在进行异步调用时在同一线程中运行。这将如何不阻塞 UI?

异步与多线程不同,JavaScript 通常不是多线程的。在 JS 中进行的任何繁重处理都会阻塞 UI,如果您想最大程度地减少对 UI 的阻塞,请尝试 网络工作者

indexedDB 允许更大的存储。为什么不增加 HTML5 商店的大小?

因为,如果没有适当的索引,它会变得越来越慢。

IndexedDB is not a key-value store in the same way that Local Storage is. Local storage just stores strings, so to put an object in local storage the usual approach is to JSON.stringify it:

myObject = {a: 1, b: 2, c: 3};
localStorage.setItem("uniq", JSON.stringify(myObject));

This is fine for finding the object with key uniq, but the only way to get the properties of myObject back out of local storage is to JSON.parse the object and examine it:

var myStorageObject = JSON.parse(localStorage.getItem("uniq"));
window.alert(myStorageObject.b);

This is fine if you only have one, or a few objects, in local storage. But imagine you have a thousand objects, all of which have a property b, and you want to do something just with those ones where b==2. With local storage you'll have to loop through the entire store and check b on each item, which is a lot of wasted processing.

With IndexedDB you can store stuff other than strings in the value: "This includes simple types such as DOMString and Date as well as Object and Array instances." Not only that, but you can create indexes on properties of the objects that you stored in the value. So with IndexedDb you can put those same thousand objects in it but create an index on the b property and use that to just retrieve the objects where b==2 without the overhead of scanning every object in the store.

At least that's the idea. The IndexedDB API isn't very intuitive.

They appear to run in the same thread as the async calls were made. How will this not block the UI?

Asynchronous is not the same thing as multi-threaded, JavaScript, as a rule, is not multi-threaded. Any heavy processing you do in JS will block the UI, if you want to minimize blocking the UI try Web Workers.

indexedDB allows a larger store. Why not increase the size of the HTML5 store?

Because, without proper indexing, it would get increasingly slower the larger it got.

千と千尋 2024-11-12 13:00:40

我发现这篇好文章讨论了 localstorage 与 indexeddb 以及其他可能的选项。

(以下所有值均以毫秒为单位)

https ://nolanlawson.com/2015/09/29/indexeddb-websql-localstorage-what-blocks-the-dom/

内存比较< /a>

总结一下这篇文章(全部是作者的观点),

  1. 在 Chrome、Firefox 和 Edge 这三个浏览器中,当您写入数据时,LocalStorage 会完全阻塞 DOM 2。这种阻塞比内存中的阻塞要明显得多, 自从浏览器必须实际刷新到磁盘。
  2. 毫不奇怪,由于任何同步代码都会阻塞,因此内存中的操作也会阻塞。 DOM 在长时间运行的插入过程中会阻塞,但除非您正在处理大量数据,否则您不太可能注意到,因为内存中的操作非常快。
  3. 在 Firefox 和 Chrome 中,对于基本键值插入,IndexedDB 都比 LocalStorage 慢,并且它仍然会阻塞 DOM。在 Chrome 中,它也比 WebSQL 慢,后者确实会阻塞 DOM,但没有那么慢。只有在 Edge 和 Safari 中,IndexedDB 才能在不中断 UI 的情况下在后台运行,更糟糕的是,这两个浏览器仅部分实现了 IndexedDB 规范。

  4. IndexedDB 在 Web Worker 中运行良好,运行速度大致相同,但不会阻塞 DOM。唯一的例外是 Safari,它不支持 Worker 内部的 IndexedDB,但它仍然不会阻塞 UI。

  5. 如果数据简单且最小,本地内存是理想的

I came across this good article discussing about localstorage vs indexeddb and other possible options.

(all the below values are in milliseconds)

https://nolanlawson.com/2015/09/29/indexeddb-websql-localstorage-what-blocks-the-dom/

memory comparison

To summarize from the article (entirely author's views),

  1. In all three of Chrome, Firefox, and Edge, LocalStorage fully blocks the DOM while you’re writing data 2. The blocking is a lot more noticeable than with in-memory, since the browser has to actually flush to disk.
  2. Not surprisingly, since any synchronous code is blocking, in-memory operations are also blocking. The DOM blocks during long-running inserts, but unless you’re dealing with a lot of data, you’re unlikely to notice, because in-memory operations are really fast.
  3. In both Firefox and Chrome, IndexedDB is slower than LocalStorage for basic key-value insertions, and it still blocks the DOM. In Chrome, it’s also slower than WebSQL, which does blocks the DOM, but not nearly as much. Only in Edge and Safari does IndexedDB manage to run in the background without interrupting the UI, and aggravatingly, those are the two browsers that only partially implement the IndexedDB spec.

  4. IndexedDB works swimmingly well in a web worker, where it runs at roughly the same speed but without blocking the DOM. The only exception is Safari, which doesn’t support IndexedDB inside a worker, but still it doesnt block the UI.

  5. localmemory is ideal if data is simple and minimal

酒与心事 2024-11-12 13:00:40

添加到 robertc 的 anwser 中,indexedDB 知道“范围”,因此您可以搜索和检索以“ab”开头并以 abd”结尾的所有记录,以查找“abc”等。

Adding to the anwser of robertc, indexedDB knows 'ranges' so you can search and retrieve all records that start with 'ab' and end with abd' to find 'abc' etc.

沧笙踏歌 2024-11-12 13:00:40

在浏览器控制台中运行以下命令。它将在“应用程序”>“应用程序”中创建一个单独的实体。与 LocalStorage 和 SessionStorage 一起存储

const request = indexedDB.open("notes");
request.onupgradeneeded = e => {
  alert("upgrade");
}
request.onsuccess = e => {
  alert("success");
}
request.onerror = e => {
  alert("error");
}

您可以使用所有 CRUD 操作查询此存储,这与其他两个存储的灵活性和功能较差不同。

Run the following in console of browser. It will create a separate entity in Application > Storage alongside LocalStorage and SessionStorage

const request = indexedDB.open("notes");
request.onupgradeneeded = e => {
  alert("upgrade");
}
request.onsuccess = e => {
  alert("success");
}
request.onerror = e => {
  alert("error");
}

You can query this Storage with all CRUD operations unlike other two storages that has lesser flexibility and functions to play with.

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