title: Indexing by id and index
body: <p>I've a lot of objects with IDs, location and some visual properties.</p>
  
  <p>I've breaking those objects to simple arrays (<code>Float32Array</code>) in order to upload them to the GPU.</p>
  
  <p>So my arrays look like this</p>
  
  <p>My vertex array [x,y]: <code>[1,1,2,2,3,3,4,4]</code>
  The data is in couples, means that the first and second are x&amp;y of the first object, and the 3rd and 4th to the second object and so on... </p>
  
  <p>In order to allow delete of objects, I need to map somehow my objects to index.</p>
  
  <p><strong>1st :</strong> I'm saving for each object its index - so I've ID to index mapping.</p>
  
  <p>My mapping object look like this: <code>{ "id1": 0, "id2": 2, "id3":4, "id4":6 }</code></p>
  
  <p>The problem is after the first delete - all the indices after it are updated.
  For example: I delete <code>id2</code>. So I find the index of it which is <code>2</code> and remove 2 items x&amp;y. The array look like this now: <code>[1,1,3,3,4,4]</code>
  My mapping of <code>id3</code> and <code>id4</code> are wrong now.</p>
  
  <p>It is OK when we have 4 objects, but my arrays are very big, 500K and more...<br>
  And updating after every delete the indices of the objects which are after in the array is causing me very bad performances.</p>
  
  <p><strong>2nd:</strong>
  I've tried also mapping it differently... As array : <code>[{id:"id1"}, {id:"id2"}..]</code>
  This way before the delete, I need to find which index it is by its ID (getting very bad when deleting the lasts objects). But not updating it after delete.</p>
  
  <p>Both ways are not good for me from performance point of view.</p>
  
  <p><strong>-- EDIT --</strong></p>
  
  <p>This is the solution that i'm using so far that gives me the best performance at the moment.</p>
  
  <p>Every Object that being added creates ids for each letter it contains. and it gets the first letter id and the count of letters it created.</p>
  
  <p>In the following example there are 3 objects with different amount of letters.</p>
  
  <p><a href="https://i.stack.imgur.com/F30L3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F30L3.png" alt="enter image description here"></a></p>
  
  <p>The objects are getting the ids of the first letter and the length.</p>
  
  <p>In order to delete object B i'm running over my ids list and finding the id of B (which is 4) and i've found it at index 3. 
  means that it's starting in my flat array at index  3 * 2(x,y) = 6. In order to delete it i would delete the length saved in the object. in this case 10 * 2(x,y) = 20 cells in the flat array. In addition i would delete 10 (the length) cells in the ids list. after delete the lists look like this</p>
  
  <p><a href="https://i.stack.imgur.com/cVRZm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cVRZm.png" alt="enter image description here"></a></p>
  
  <p>This is the fastest way I've found in order to delete over and over objects.</p>
  
  <p>After clients complains that this is too slow, I've started to collect objects to delete and delete them all together - which mean - server is updating my clients objects - He running and deleting object by object in a loop - instead of deleting the objects immediately from the arrays i'm collecting them, and running once over my Ids list and finding all the ids indices - sorting them and deleting accordingly all together. </p>
  
  <p>But this cause some other problems that i'm always finding some workaround.</p>
  
  <p>Can't say that i'm happy with that.</p>
  
  <p><strong>-- END EDIT --</strong></p>
  
  <p>Any suggestions for better mapping?</p>