简体   繁体   English

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

[英]How is indexedDB conceptually different from HTML5 local storage?

  1. Both indexedDB and local storage are key value stores. indexedDB和本地存储都是键值存储。 What is the advantage of having two key/value stores? 拥有两个键/值存储的优势是什么?
  2. indexedDB is asynchronous, but joins (the most time-consuming thing) are manual. indexedDB是异步的,但是连接(最耗时的东西)是手动的。 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. indexedDB允许更大的商店。 Why not increase the size of the HTML5 store? 为什么不增加HTML5商店的大小?
  4. I'm scratching my head. 我在挠头。 What is the point of indexedDB? indexedDB有什么意义?

IndexedDB is not a key-value store in the same way that Local Storage is. IndexedDB不是与本地存储相同的键值存储。 Local storage just stores strings, so to put an object in local storage the usual approach is to JSON.stringify it: 本地存储只存储字符串,因此要将对象放在本地存储中,通常的方法是JSON.stringify它:

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: 这对于使用键uniq查找对象很好,但是将myObject的属性从本地存储中取出的唯一方法是使用JSON.parse对象并检查它:

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 . 但是想象你有一千个对象,所有这些对象都有一个属性b ,你想要用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. 使用本地存储,您将需要遍历整个商店并检查每个项目的b ,这是一个浪费很多的处理。

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." 使用IndexedDB,您可以在值中存储字符串以外的东西 :“这包括简单类型,如DOMString和Date以及对象和数组实例。” 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. 因此,使用IndexedDb,您可以在其中放置相同的数千个对象,但在b属性上创建索引并使用它来检索b==2的对象,而无需扫描存储中的每个对象。

At least that's the idea. 至少这是个主意。 The IndexedDB API isn't very intuitive. IndexedDB API不是很直观。

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 . 异步与多线程不同, JavaScript通常不是多线程的 Any heavy processing you do in JS will block the UI, if you want to minimize blocking the UI try Web Workers . 如果你想最小化阻止UI尝试Web Workers ,你在JS中做的任何繁重的处理都会阻止UI。

indexedDB allows a larger store. indexedDB允许更大的商店。 Why not increase the size of the HTML5 store? 为什么不增加HTML5商店的大小?

Because, without proper indexing, it would get increasingly slower the larger it got. 因为,如果没有适当的索引,它会变得越来越慢。

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

I came across this good article discussing about localstorage vs indexeddb and other possible options. 我遇到了这篇讨论localstorage vs indexeddb和其他可能选项的好文章。

(all the below values are in milliseconds) (以下所有值均以毫秒为单位)

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

记忆比较

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. 在Chrome,Firefox和Edge的所有三个版本中,LocalStorage在您写入数据时完全阻止了DOM。阻塞比内存更加明显,因为浏览器必须实际刷新到磁盘。
  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. 在长时间运行的插入过程中,DOM会阻塞,但除非您处理大量数据,否则您不太可能注意到,因为内存中的操作非常快。
  3. In both Firefox and Chrome, IndexedDB is slower than LocalStorage for basic key-value insertions, and it still blocks the DOM. 在Firefox和Chrome中,IndexedDB比LocalStorage更慢,用于基本键值插入,并且它仍然阻止DOM。 In Chrome, it's also slower than WebSQL, which does blocks the DOM, but not nearly as much. 在Chrome中,它也比WebSQL慢,后者阻止了DOM,但几乎没有那么多。 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. 只有在Edge和Safari中,IndexedDB才能在不中断用户界面的情况下在后台运行,并且更加严重的是,这两种浏览器只能部分实现IndexedDB规范。

  4. IndexedDB works swimmingly well in a web worker, where it runs at roughly the same speed but without blocking the DOM. IndexedDB在Web工作者中运行良好,它以大致相同的速度运行,但不会阻止DOM。 The only exception is Safari, which doesn't support IndexedDB inside a worker, but still it doesnt block the UI. 唯一的例外是Safari,它不支持worker中的IndexedDB,但它仍然不会阻止UI。

  5. localmemory is ideal if data is simple and minimal 如果数据简单且极少,本地内存是理想的选择

Run the following in console of browser. 在浏览器控制台中运行以下命令。 It will create a separate entity in Application > Storage alongside LocalStorage and SessionStorage 它将在LocalStorage和SessionStorage中的Application> Storage中创建一个单独的实体

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. 您可以使用所有CRUD操作查询此存储,这与其他两个具有较低灵活性和功能的存储不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM