joshnuss / svelte-persisted-store

A Svelte store that persists to localStorage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optimization possible

mschwartz opened this issue · comments

https://github.com/joshnuss/svelte-local-storage-store/blob/34b1994bd22180d704d7629938e9b27936aa375e/index.ts#L50

You are creating an event handler and capture for every store, plus adding a listener for it.

I don’t think you need more than one listener. The Event object passed to the handler includes the key, and you have the map of key to store already.

This optimization can save memory and be more performant , IMO.

Thanks @mschwartz!

You are right, it can totally be defined once.

Though it will require some refactoring, since the event sends the new value serialized, but it will need to be deserialized according to the serializer option of the store (it's not always JSON).