javascriptobjectoptimizationobject-properties

Difference between Object property accessing in Javascript using key or loop


I have a linear(or single dimension) object with more than 1000 properties within a single record. I have to traverse more than 10000 records. It is possible that sometime the required property doesn't contain by the object within a single record. I want to know what would be the better strategy to traverse them either by looping all the properties of an object or directly check if the key is available in the object by referencing it. Please, check below example to understand this better.

Let's consider an example scenario:

var a = {
  "x": 1,
  "y": 2,
  "z": 3,
  "t": 4
}; //linear object
var flagA = false;
var b = {
  "x": 10,
  "y": 11,
  "z": 12
}; //linear object
var flagB = false;


//now I have to check the value of "t" in both objects.

//----------------------------------By use of looping-------------------------------//
for (var i in a) {
  if (i == "t") {
    flagA = true;
    break;
  }
}

for (var i in b) {
  if (i == "t") {
    flagB = true;
    break;
  }
}

//for object a
if (flagA) console.log("value found in object a for t:", a.t);
else console.log("value not found for t in object a");

//for object b
if (flagB) console.log("value found in object b for t:", a.t);
else console.log("value not found for t in object b");


//--------------------------------------------------------------------------------------//
console.log("\nANOTHER METHOD\n");
//-----------------------------------By use of Key-------------------------------------//

//for object a
if (a["t"]) console.log("value found in object a for t:", a["t"]);
else console.log("value not found for t in object a");

//for object b
if (b["t"]) console.log("value found in object b for t:", b["t"]);
else console.log("value not found for t in object b");
//--------------------------------------------------------------------------------------//

Which one method should I use and Why?


Solution

  • Looping is definitely unnecessary here, especially since you're dealing with a large number of properties on your objects - checking whether a property exists is an O(1) operation, whereas looping through all properties and searching for a particular match is an O(N) operation. So your second method is definitely better - it'll require less time, and the code is more readable.

    That said:

    if (a["t"]) console.log("value found in object a for t:", a["t"]);
    

    is not a good test, because this will fail if the property exists, but the value is falsey.

    const a = {
      t: 0
    };
    
    if (a["t"]) {
      console.log("value found in object a for t:", a["t"]);
    } else {
      console.log('not found!');
    }

    Use hasOwnProperty instead:

    if (a.hasOwnProperty('t'))
    

    var a = {
      "x": 1,
      "y": 2,
      "z": 3,
      "t": 4
    };
    var b = {
      "x": 10,
      "y": 11,
      "z": 12
    };
    
    const requiredProps = ['x', 't'];
    const verify = obj => requiredProps.every(prop => obj.hasOwnProperty(prop));
    
    console.log(
      verify(a),
      verify(b)
    );