4

I have an situation where I have 3 different arrays with very different amounts of objects in it. I've read many questions and blog posts about this but Im still unsure when to use what.

PS! My biggest problem is that I need to iterate and push (perfect for arrays), also find if exists in array and delete (more suitable for objects). Specific order is not required.

I can't allow having same object in both array1 and array1clicked because they should perform different actions.


When it's best to use object and when array in my example? What should I replace with object and what should stay as array? Im pretty sure that amounts of objects in it also matters, right?


My current code:

//Objects in arrays are literally custom {objects} with custom prototypes and html
var array1 = [ 20 objects ];
var array1clicked = [];

var array2 = [ 250 objects ];
var array2clicked = [];

var array3 = [ 50 000 objects ];
var array3clicked = [];

//Each object in arrays has event attached
objecthtml.click(function() {
    //Add to clicked array
    array1clicked.push(thisobject);
    //Remove from initial array
    var index = array1.indexOf(thisobject);
    if (index > -1) {
        array1.splice(index, 1);
    }
}
//Same with array2 and array3 objects


//Iterations on different conditions
var array1count = array1.length;
var array1clickedcount = array1clicked.length;

//Same with array2 and array3

if(condition1) {
   for(a = 0; a < array1count; a++) {
       array1[a].div.style.visibility = 'hidden';
   }
   //Same with array2 and array3 objects

   for(a = 0; a < array1clickedcount; a++) {
       array1clicked[a].div.style.visibility = 'visible';
   }
   //Same with array2clicked and array3clicked objects
}
else if(condition2) {
   for(a = 0; a < array1count; a++) {
       array1[a].div.style.visibility = 'visible';
   }
   //Same with array2 and array3 objects

   for(a = 0; a < array1clickedcount; a++) {
       array1clicked[a].div.style.visibility = 'hidden';
   }
   //Same with array2clicked and array3clicked objects
}
2
  • 1
    If it needs to be sorted or may not have unique keys, you should use an array. If you may need to iterate over it, use an array. If none of the above fits your use case and you have unique keys for each object, use an object. Looks like you need to use an array. and... use theArray.forEach please. Commented Jan 25, 2016 at 22:18
  • Why do you need multiple data structures instead of setting a flag property on the objects/elements? Even with 50,000 iteration performance over the whole list with conditionals should be acceptable if triggered by user-interaction. You have 100ms...this seems like premature optimization. Commented Jan 25, 2016 at 22:36

2 Answers 2

6

It seems you want a data structure with these operations:

  • Iteration
  • Insert
  • Delete
  • Search

With arrays, the problem is that searches and deletions (with reindexing) are slow.

With objects, the problem is that the property names can only be strings.

The perfect structure is a set.

var s = new Set();
s.add(123); // insert
s.has(123); // search
s.delete(123); // delete
s.values(); // iterator
Sign up to request clarification or add additional context in comments.

2 Comments

That's very interesting, I need to learn a lot because I don't know nothing about it. I wonder why it was never mentioned after that much research.. Does it have drawbacks? Iteration is adviced?
@Solo Probably you didn't know about it because it's a recent addition to the ECMAScript spec. This also means it won't work on old browsers, but you can polyfill it, e.g. with es6-shim. See MDN if you want to know more about sets. You may be interested in weaksets or maps too.
0

In your case, I think you have to use just Array.

In common case, you could use object to keep references and push some values into it, but If you wanna iterate on this, I think you have to use Array.

8 Comments

You can iterate object properties too
Object.keys(someObject).forEach(...
Yeah, you right, thanks. But do you know what is the most powerful in this case ? It depends of the size of your array/object? ?
@Oriol How about object iteration performance with 50 000 properties? I could easily replace array1 (20 properties) and array2 (250 properties) with objects but Im afraid array3 with 50 000 properties is too much, right?
@Oriol, do you often use set() class ? And In which context ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.