feathersjs-ecosystem / feathers-localstorage

A client side service based on feathers-memory that persists to LocalStorage
MIT License
38 stars 15 forks source link

improve performance by not to save/load the whole data everytime #31

Closed beeplin closed 8 years ago

beeplin commented 8 years ago

It seems that each time we read/write to this service, it load/save the whole big string from localstorage, which might lead to big performance problem when the string is very very long.

It could be improved by not to save all data into a single localstorage item name, but to save different items based on each record's id. Then we just need to call localstorage.getItem(id) to get, and localstorage.setItem(id) to create/update/patch, localstorage.removeItem(id) to remove.

BTW, we could also consider making an interface for localforage, a wrapper and extension for localstorage.

daffl commented 8 years ago

The reason is that the adapter can rely on feathers-memory for everything except for writing and reading from localStorage. Writes are throttled so if you make 100 changes at once it will only cause one write. The adapter also only reads from localStorage once during initialization. I would argue that it is in general probably faster than writing and reading individual items from localStorage every time.

If you are still concerned about write performance you can set the throttle option from the default of 200ms to a higher value, e.g. 5000 which will only write every 5 seconds (if anything changed at all).

beeplin commented 8 years ago

yes that makes sense. Localstorage itself has very limited data size, so loading the whole thing into memory should be OK. :)