Home > Net >  Concerning memory: Store a json data on a service or get it whenever you need?
Concerning memory: Store a json data on a service or get it whenever you need?

Time:04-07

I have an Ionic/Angular quiz app that uses a .json file supplying an array of questions.

Before the game start I take these questions and do the logic inherent to the application. Like randomize the questions etc.

The user only sees one question at a time. And I'm using a component with id parameter (question id).

{
    path: `question:id`,
    loadChildren: () => import('path.to.module').then( m => m.Module)
},

So when the user moves on to the next question, I get the id and show the new question to the user.

My doubt is: In terms of memory, as this json is relatively big. I would like to know if it is better to get this file once and store it on a service or get it everytime the question page component load, and do the "magic".

By the way, please don't worry about the logic behind the random proccess, etc. I did my homework here :D Thnx

CodePudding user response:

In terms of memory it probably doesn't matter that much as the browser will immediately cache that .json file after the first request, and it will reside there for most likely the duration of the session - making file load time faster.

The concern here is the time to deserialize the .json file into an object. You could load, deserialize and extract a single question each time, however deserializing a large object is time consuming and I would recommend only doing this the once at the start of the application run time.

When you think about it, the total memory consumption is still going to be the same irrespective of when you deserialize the .json file as the file size is constant.

Another factor is memory pressure which can be a consideration depending on the SOE you are deploying to.

  • Related