Search code examples
javascriptjsoncachingweb-workerpersistent

Web worker with persistent data


I am building a web application with large amounts of json data.

The data will be filtered and shown in a table and ideally I would like the filtering to be done by a web worker.

However, from what I've read transferring json data between the parent and the worked also causes overhead so I am wondering:

Is it possible for a worker to load the json data once, cache it and then make the data persistent between calls?

  1. During page load the parent created a worker
  2. Then it asks the worker to load and cache the json data
  3. When the user inputs different filters the parent can ask the worker to return only the relevant data.

Is this possible, and does it make sense?

/Patrik


Solution

  • Workers can use IndexedDB for persistent storage.

    Workers can’t use Web Storage (localStorage) but there are good small IndexedDB libs, like Dexie—which can be used in workers & which makes using IndexedDB nearly as easy as localStorage.