0

I am developing a app in React + Redux and I have a constant doubt and I can't find documentation about it. Is there any performance downside if, let's say in a saga, I read data from a cookie/localStorage instead from the state? This read process would only happen once on each load.

The key thing here is the performance, without taking into consideration if it's good or bad practice.

Thank in advance.

Eneko
  • 139
  • 1
  • 2
  • 12

1 Answers1

1

First of all - what do you mean state ? In redux - state is just a plain object (plus some methods, but still). So when you read data from there - you just read props from object.

While cookies, localstorage - it's DOM api, which first of all slower, plus you need not only read data, but also parse it (cause both cookies, storage work with serialized data). So definitely storage/cookie slower than state.

You can check http://jsben.ch/nvo5G

BUT! - you can't save in-memory object state between page reloads. So for this, you can use storage (pattern named persistent state. And there is probably no other way to implement this functionality (or client DB) - in case you need to restore some state on reload - you have just two options - save state on a client (cookies, storage/db), or on server (and do fetch request).

It's MICRO optimisations, mostly you shouldn't care about it (in the case of reading just on start app)

Vasiliy Vanchuk
  • 6,618
  • 2
  • 19
  • 43
  • Thanks for the answer. Yes, in deed I meant redux-state, which BTW I know is a plain object. Maybe, the correct way to ask the question would be to ask "what are the differences between reading a basic object (like a numbre) stored _in memory_ (like it can be the state in this case) and reading it from a cookie/localStorage", just to skip the parsing thing. Anyway, it's interesting to hear that DOM api is slower. Do you have any docu where I can refer to? Thanks again! ;-) – Eneko Jun 10 '19 at 15:34
  • @Eneko it's MICRO optimization - mostly you shouldn't care about such things (in the case on reading just on start app) - comparison you can find here http://jsben.ch/nvo5G – Vasiliy Vanchuk Jun 10 '19 at 20:25
  • Thanks again!! That benchmark is quite clarifying. Also, you're right it should not be something to worry about for a single read in the start app, anyway it was something that I was very curious about. Any improvement in the performance is welcome anyway ;-) – Eneko Jun 10 '19 at 20:38