XF 2.1 Best way to loop through json, template side or controller side?

Dannymh

Active member
Hi,

I have an add-on I am making which takes a complex and somewhat poorly ordered json file, I am looking at the least memory load and process intensive way to manage the json file and wondering if you can help.

My two options thus far are

loop through the json array and build a better ordered array, then parse that array to the template for ordering and processing, this will mean that I essentially loop through the data 3 times, one for section 1, once for section two, then once in the template with the new array

The second option is that I parse all that data to the template and do the looping and sorting on the template side, which really means only two loops through the data.

My question I guess is more around whether doing so on the template side is computationally more expensive that doing it in the controller before parsing to the view?

For more context, the json is a lot of game data from a sport, so it lists the players, the players stats, the teams and a few more things, but it is not ordered logically say by team, where you could say grab all players from team one and their stats and then output that, so I have to do some work to pull that all together so players line up with team and the stats line up with the player. I can do that part easily enough but I want to do it cheaply as the page it is displayed on gets very active users during a game and I don't want to slow their load times.

Daniel
 
Does the json file change per page load? My suggestion would be to parse and sort it after fetching it and cache the result for as long as possible. Depending on your page visits, even a couple of seconds may pay off. I don't think it's possible to sort an array in templates, so your only other option would be to do it on the client side.
 
Back
Top Bottom