简体   繁体   中英

Storing in javascript array vs. (LocalStorage and IndexDB)

How is storing values directly in Javascript arrays is different than from storing them in come client side alternative like LocalStorage or IndexDB.

Where the values get stored in either cases. And what are limits.

My use case is: to store very fast data (float values) coming from server somewhere and then read it from that middle source to render the points on a real time plot/graph. Does the frequency also makes a difference to the choice ?

Any sample code snippet will be of great use.

Obvious differences between localStorage and arrays are :

  • you can only store strings (no objects)
  • it's persisted so that you'll get your values back next time
  • as it writes on disk it's a heavier operation
  • the available space is limited to avoid cluttering the user's disk ( "User agents should limit the total amount of space allowed for storage areas.[...] A mostly arbitrary limit of five megabytes per origin is recommended" )

W3.org reference on localStorage

You read and write in localStorage like this :

var foo = localStorage["bar"];
// ...
localStorage["bar"] = foo;

(example taken from the great introductory site http://diveintohtml5.info/storage.html )

If you're only using your data for the current session, don't use localStorage . Use standard arrays (they're fine and fast) or Float32Array .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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