title: Best performance choice for cookie arrays in PHP and JS?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>The code below is not real code, it's just to make you understand the differences between the options.</p>
  
  <p><strong>Option 1</strong></p>
  
  <p>one cookie called 'watched' which is a JSON encoded array containing the IDs. I would need to check if the array contains that value</p>
  
  <pre><code>$_COOKIE['watched'] = array(1,2,3,4,...)
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>Option 2</strong></p>
  
  <p>one cookie for each ID, called for example "watched_[ID]". I would just check if it exists or not.</p>
  
  <pre><code>exists( $_COOKIE['watched_1'] ) ? watched : !watched
  exists( $_COOKIE['watched_2'] ) ? watched : !watched
  </code></pre>
  
  <p><strong>Option 3</strong></p>
  
  <p>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</p>
  
  <pre><code>$_COOKIE['watched'] = array(
                 1 =&gt; array(1,2,3),
                 2 =&gt; array(4,5,6)
  )
  </code></pre>