title: How to keep Javascript array sorted, without sorting it
body: <p>I have a Node.js application where I have to very often do following things:
  - check if particular array already contains certain element
  - if element does exist, update it
  - if element do not exist, push it to the array and then sort it using underscore _.sortBy</p>
  
  <p>For checking if the element already exists in the array, I use this binary search function:
  <a href="http://oli.me.uk/2013/06/08/searching-javascript-arrays-with-a-binary-search/" rel="noreferrer">http://oli.me.uk/2013/06/08/searching-javascript-arrays-with-a-binary-search/</a></p>
  
  <p>In this way, when the size of the array grows, the sorting becomes slower and slower.
  I assume that the array size might grow to max 20 000 items per user. And eventually there will be thousands of users. The array is sorted by a key, which is quite a short string. It can be converted into integer if needed.</p>
  
  <p>So, I would require a better way to keep the array sorted,
  in stead of sorting it every time new element is pushed onto it.</p>
  
  <p>So, my question is, how should/could I edit the binary search algorithm I use, to enable me to 
  get the array index where the new element should be placed, if it doesn't already exist in the array?
  Or what other possibilities there would be to achieve this. Of course, I could use some kind of loop that would start from the beginning and go through the array until it would find the place for the new element.</p>
  
  <p>All the data is stored in MongoDB.</p>
  
  <p>In other words, I would like to keep the array sorted without sorting it every time a new element is pushed.</p>