The best performance choice for cookie tables in PHP and JS?

advertisements

Hi I have a website with hundreds of tv shows and tens of thousands of episodes. I want the user (who didn't log in) to be able to save his favorites shows and to mark the episodes he's watched and save these data using cookies.

So I've already implemented it both in PHP and JS. I just wanted to know what's the best choice when it comes to arrays of cookies.

Now I save the data in a cookie called 'favorites', which is a JSON encoded array which simply contains the IDs of the tv shows. In the cookie 'watched' there are the IDs of the episodes that the user has marked as watched.

What's the best option for performance and memory? You must keep in mind this is also intended for mobiles so, if the user marks thousand of episodes as watched, I don't want this to slow down everything.

The code below is not real code, it's just to make you understand the differences between the options.

Option 1

one cookie called 'watched' which is a JSON encoded array containing the IDs. I would need to check if the array contains that value

$_COOKIE['watched'] = array(1,2,3,4,...)

With this option, in the tv-show page I would always have to pick the whole array and check if every single episode ID is a value in the array.

Option 2

one cookie for each ID, called for example "watched_[ID]". I would just check if it exists or not.

exists( $_COOKIE['watched_1'] ) ? watched : !watched
exists( $_COOKIE['watched_2'] ) ? watched : !watched

Option 3

one cookie called 'watched' which is an associative JSON encoded array containing an array for each tv show, called for example 'watched[show_ID]', and each one of these contain only the IDs of the episodes marked as watched belonging to that show. (so in the tv-show page I first check for 'watched[show_ID]' then I already have all the watched episodes, instead of comparing every single episode ID with the array 'watched' from Option 1

$_COOKIE['watched'] = array(
               1 => array(1,2,3),
               2 => array(4,5,6)
)


The Approach you select will purely depend on the type of users you have, if data size generally is large in your case then you should go with option 3 as associative arrays are a good approach when it comes to large data size as it will be somewhat easy to loop through them and get the results.

If your users data set is fairly small then option 2 is the best approach as we have different cookies for each shows and you could immediately check the corresponding values.

Further I would like you to go through the html5 api to use indexed db as a solution to your problem as it provides client side DB and further it is a persistent database. Hope this helps