0

I am making a collision detector in JavaScript. I want to make a tree structure so that I can make a complex object.

There is one big array and all the objects including children are part of the array. Some of them are child and others are parents. Each item has children, parent and root property and if a collision happens, it bubbles up to the parent.

I came up with 2 ways to identify objects.

  1. Give objects IDs

  2. Quote object directly like "node1.root = someOtherObject".

I also want to know which is faster

//1
if(object1.root === root.id){/*code here*/}//id based identification (literally)
        //19253 === 19253

//or 2
if(object1.root === root){/*code here*/}//object based identification
     //[Object] === [Object]
parwatcodes
  • 6,669
  • 5
  • 27
  • 39
martian17
  • 418
  • 3
  • 14
  • It probably won't make much of difference, if any. I'd say just use what you think is the easiest to maintain and most readable. – elclanrs Feb 22 '17 at 05:15
  • two objects can't be equal `{} === {}` output `false`. So consider giving property in the object. – Jai Feb 22 '17 at 05:18
  • The difference in performance will be negligible. I think comparing object references is technically the right way to go, but you might end up wanting ids as well just for debugging purposes. See [this](http://stackoverflow.com/a/886053/74757) for further explanation. – Cᴏʀʏ Feb 22 '17 at 05:19
  • I am going to compare if the objects are exactly the same. I understand that '({}==={})===false" and "({"bar":"foo"}==={"bar":"foo"})===false' – martian17 Feb 22 '17 at 05:22

1 Answers1

1

Both will be same in respect of performance. Please check below link.

http://jsben.ch/#/Y9jDP

Vikash Sinha
  • 193
  • 8