2

In my chrome extension, I would like to store files in the browser (from background script) but not on disk, as I would like to read and erase the files quickly. Is the best solution to use blob URLs? If so, how much data can I store as blob URLs? Or should I just store the file contents in an array in the background script?

Moe Bazzi
  • 85
  • 3

1 Answers1

2

Blob is probably the best choice. They act like files- the File() constructor inherits properties from it. Arrays are memory and resource intensive for large data storage, but an Int8Array or Uint8Array would probably work if necessary, too. TypedArrays are generally faster than regular arrays. Blobs can have sizes greater than 128 MB (it varies across platforms, from 128 MB to 640 MB), which would probably fill most requirements. However, if the memory isn't available, and in other rare cases, the Blobs will be written to disk. I don't think Blobs were designed for high amounts of writing.

user1280483
  • 470
  • 4
  • 11
  • If the blob is too large and is forced to be written to disk, can it still be accessed via the blob url? – Moe Bazzi Dec 19 '21 at 19:14
  • Of course- but it will be slower, and disk-based. I'm assuming you don't want that, as you said fast access. – user1280483 Dec 19 '21 at 19:15
  • Thanks. I'm considering storing the blobs in indexDB so they can persist through extension refreshes. Only thing is I wish you were able to access the same indexDB from content script and background script. – Moe Bazzi Dec 19 '21 at 21:31
  • The problem with `indexedDB` is that it's disk access _every_ time you want to read/write, so it's going to be pretty slow. – user1280483 Dec 19 '21 at 23:27
  • right. So what I'll do is create an object url of the blob to be used in the application during the extensions lifetime, and also save the blob to indexDB so I can fetch the file from the indexDB after an extension refresh only, and then create a blob url again to be used during this lifetime. – Moe Bazzi Dec 20 '21 at 22:04