I'm building an javascript application where I need to register devices to an specific room. So I thought, I'm making my room_name as main key (room name is unique). And I'm going to add the devices under that key.

I want to have this setup:

room name 
    --> devices
          --> device 1
          --> device 2

The code to achieve this:

var obj = {};

if(!obj['testRoom']) {
  console.log("testRoom does not exist");

  obj['testRoom'] = {};
  obj.testRoom.devices = [];

//add devices to room
obj.testRoom.devices.push({id: 1, name: "Dev 1", key: "testKey"});
obj.testRoom.devices.push({id: 2, name: "Dev 2", key: "testKey2"});

The output is as expected. But I'm a little worried about the performance (In this case finding a room on the room_key, I know to room that I want to search for). I'm expecting 2000 individual rooms, and somewhere between 3 devices per room. Is this the best way to do it or are there better ways?


I have tested 2 methods for searching the room with the room key: With key in object and object[key]. The results are very similar

100000 elements
key in object:
Call took 13681.915 milliseconds. 
Call took 13775.595000000001 milliseconds.

Call took 13702.735000000002 milliseconds. 
Call took 14111.225000000002 milliseconds.

Key in object is a little bit faster. Are there better ways to search the room as fast as possible?

  • You would have to use an appropriate data structure for this if you want performance. But this depends on the use cases you have. Maybe a trie... pthurlow.github.io/triejs – caisah Feb 26 '17 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.