-2

So I have a bunch of (10000+) objects that I need to remove/replace for simplicity, we can presume that the object contains a (String) Unique 'id'. The items often need to be renamed (change id), but not as often as it's looked up

{ id: 'one' }, { id: 'two' }, ...

If I place them inside an 'associative array' (bad practise), I can access them quickly, but need to loop (slow) to remove (NOTE: This doesn't actually work, because findIndex only works correctly on proper arrays, but a for loop would do the same thing)

arr = [];
arr['one'] = { id: 'one' };
arr['two'] = { id: 'two' };
arr.splice(arr.findIndex(function(i) { return i.id === 'one'; }), 1);

If I place them in a normal array, I have to loop (slow) to find the item by ID, and deleting would require a loop (slow) as well (Edit: In my particular case deleting it should be relatively quick as I'll have already looked it up and have a reference, but obviously slower if I lose reference)

arr = [{ id: 'one', }, { id: 'two' }];
arr.splice(arr.findIndex(function(i) { return i.id === 'one'; }), 1);

or, if I store them the obviously correct way, I have the choice of using the delete keyword (which I've always been told is slow and breaks optimisations), or setting as undefined (which leaves me with a lot of elements that exist - memory leaks? and slower loops)

 obj = { one: { id: one }, two: { id: two } };
 delete obj['one'];

...

 obj = { one: { id: one }, two: { id: two } };
 obj['one'] = undefined;

I'm thinking that delete object[...] is the best choice, but I'm interested in other's feedback. Which should I use, and why?

JD Byrnes
  • 783
  • 6
  • 17
  • delete releases some memory also. – Alex Jun 28 '16 at 21:10
  • 1
    Can't use `array.indexOf({ id: 'one' }` ... can only match same object reference. Same as `{ id: 'one' }=={ id: 'one' }` is false – charlietfl Jun 28 '16 at 21:12
  • please change the name of `array` when it is an object. – Nina Scholz Jun 28 '16 at 21:13
  • See [this answer](http://stackoverflow.com/a/27397763/3149020). It's not that delete is slow necessary, but the entire structure of the object is changed along with memory allocation. That is what is affecting the performance. It isn't something you can get around. – Spencer Wieczorek Jun 28 '16 at 21:14
  • note that JS has no concept of "associative arrays" – Hamms Jun 28 '16 at 21:22
  • @SpencerWieczorek That delete refers to an array becoming slow because of an Array's internal structure, rather than a delete on an Object. Am I missing something? – JD Byrnes Jun 28 '16 at 21:29
  • @JoshuaDavison My point is that it's not that delete is slow, the affects of removing an item from a data structure is what can cause performance problems. – Spencer Wieczorek Jun 28 '16 at 21:34
  • 1
    @Joshua Davison deleting properties is slow and causes some behind the scenes slowdown later on. You might want to use `Map` objects which provide you with whole API to set, delete, clear all keys etc. In normal objects it is preferred to just set the desired object property to `undefined` :) – Azamantes Jun 28 '16 at 21:58
  • @Azmantes After reading about Map on MDN, I think that it is a perfect candidate for the situation provided. Could you please offer an Answer? Thanks. – JD Byrnes Jun 28 '16 at 22:22

1 Answers1

0

There's a difference in the three methods.

Array.splice removes an object, and pushes every element after this 1 back, so the indexing doesn't get cut.

Delete tries to delete an object, but may fail. This doesn't free up memory, it only breaks the reference. The garbage collector can free up the corresponding memory later.

Setting a variable to undefined pretty much marks the object to deletion for the garbage collector. It won't happen instantly, only whenever JavaScript feels so. If you set enough variables to undefined, then this method pretty much achieves the same thing as deleting the objects.

Setting a variable to undefined is not the same as deleting it, if you use delete, the you may encounter errors, when you try to reach that variable again, this won't happen when you set it to undefined.

Bálint
  • 4,009
  • 2
  • 16
  • 27
  • 1
    Why might delete fail? You mention delete breaks the reference, wouldn't the GC pick that up the same as undefined? how/why will delete cause errors if I try to use (set) the variable again? Setting to undefined leaves me with a broken property (that's found in loops) - I understand the GC will clean up here, but won't the property name, etc still eat up memory over time? – JD Byrnes Jun 28 '16 at 21:37
  • @JoshuaDavison "Throws in strict mode if the property is an own non-configurable property (returns false in non-strict). Returns true in all other cases.". It picks it up the same way. It only causes errors, if you try to use it. If you delete it, then it completely removes that variable. It won't be defined. If you set it to undefined, you would still have an undefined variable (undefined =/= not defined) – Bálint Jun 28 '16 at 21:52