In general, the first approach was such

public entities: Entities.GameEntity[] = []; public players: Entities.PlayerEntity[] = []; 

When I need to find an entity I do it like this.

 public findPlayer(playerId: string) { let playerFounded: Entities.PlayerEntity = null; this.players.forEach((player: Entities.PlayerEntity)=> { if (player.client.socketId == playerId) { playerFounded = player; } }); return playerFounded; } 

The second approach was

 public entities: any = {}; public ids: number[] = []; 

Adding

 entityParams[0] - id of number this.entities[entityParams[0]] = entity; this.ids.push(Number(entityParams[0])); 

Bypass all

 this.ids.forEach((entityId)=> { this.entities[entityId] }); 

well, if you need to quickly get that this.entities[entityId]

Doing such things as best you can, and which of these approaches is better.

  • it all depends on the task: if filters are applied, then both methods are not very good with a large amount of data - Grundy
  • and you can specifically say what you want to keep? Just not knowing what approach to development you chose and under what technology you write, it is difficult to advise something specific. It even seems strange to me that I recommend something to you at all :) - user220409

2 answers 2

Such a problem was solved by creating a special class, the Collection

IKey helper interface

 interface IKey { key: string; } 

Collection interface

 interface ICollection<T extends IKey> { add(value: T); get(key: string): T; remove(key: string); } 

And the implementation itself. The index of the element in the array is stored in the hash, so you do not need to do a full array search.

 class Collection<T extends IKey> implements ICollection<T> { private array: T[] = []; private keyToIndex: {[key: string]: number} = {}; add(value: T) { this.array.push(value); this.keyToIndex[value.key] = this.array.length - 1; } get(key: string): T { return this.array[this.keyToIndex[key]]; } remove(key: string) { this.array.splice(this.keyToIndex[key], 1); delete this.keyToIndex[key]; } } 
  • I like this better, read better - Serge Esmanovich
  • but what if you need an index by name, but I understand, I just add private keyToIndex: {[key: string]: number} = {}; - Serge Esmanovich

If the amount of data to be stored in the array is small, you can choose any option.

But if the data is large, then there will be performance problems. I am developing applications for mobile devices (cordova), and so there I had these problems (the array had about 1500 elements). And to solve them, we came up with such a trick:

 // recordKeyName {string} ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹ΠΉ ΠΊΠ»ΡŽΡ‡ // record {any} элСмСнт // records {any[]} массив для ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ setMapping: function (recordKeyName, record, records) { if (!records['maps']) { records['maps'] = {}; records.getRecordByKey = function (link) { return records.maps[link]; }; } records['maps'][record[recordKeyName]] = record; } 

 // индСксированиС массива for (var i = 0; i <= items.length - 1; i++) { this.setMapping('LINK', items[i], items); } 

In addition to this, we also index additional fields so that filtering happens faster (in the example this is not)

As a result, it turned out that with the usual viewing of the array, the time took about ~ 33 ms, and through the indices ~ 0.08

  • what is the point of setting the records.isMaps = true; flag records.isMaps = true; ? - Grundy
  • He did not need it, I forgot to remove it. I used it to determine whether array supports indexes or not - akrasnov87
  • at the same time, var me = this; too, still not used anywhere - Grundy