Till now, my server has handled huge arrays of entities.
Mainly, it does a for loop x times per second on entities, checking if one entity is in the scope of an other entity (each entity has an array entitiesInScope) by doing entitiesInScope.indexOf(entity), and that's the biggest cost of my program.
for (var i = 0; i < entities.length; i++) {
var entity = entities[i];
for (var j = 0; j < entities.length; j++) {
var checkEntity = entities[j];
var idx = entity.entitiesInScope.indexOf(checkEntity);
if (idx >= 0) {
if (!check(checkEntity.state, entity.state)) {
entity.removeEntityInScope(idx);
//... remove: send checkEntity.id
}
} else {
if (check(checkEntity.state, entity.state)) {
entity.addEntityInScope(checkEntity);
//... add: send checkEntity.id, checkEntity.state
}
}
}
}
(I've optimized it by not doing the second loop on all the entities, but that's not the point)
However, I've seen that Object's hasOwnProperty is much faster than indexOf. On the other side, I also do a lot of push and splice. So if I add an Object of entities, I should use delete (perf?) too.
Should I:
-add an object with id entity key, to use hasOwnProperty(entity.id), and then allow to use indexOf if true?
-add an object with entity key, to use hasOwnProperty(entity.id) (memory wasting)?
-continue with arrays