1

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?

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
sagar
  • 732
  • 1
  • 6
  • 25
  • Try to use a base line thinking. If you know a specific property which should be available in the object, then why go into all the pains of looping through other properties? – Krishna Prashatt Aug 07 '19 at 05:12
  • yes, I know. But all the examples I saw was all related to for looping while accessing object property. I wanted to know directly accessing of properties is optimized or should I always use for loop for this scenario. – sagar Aug 07 '19 at 05:38

1 Answers1

2

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)
);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • But hasOwnProperty will return true in case value of that particular property is null or undefined. And my motto is to have some value instead of null, undefined. – sagar Aug 07 '19 at 05:19
  • What do you count as empty? 0, `NaN`, `false`, and the empty string will all fail the `a["t"]` test (in addition to `null` and `undefined`). But if that won't cause problems, feel free to use it. – CertainPerformance Aug 07 '19 at 05:21
  • removed "empty" as it was chaotic. But there is possibility that b["t"] contain undefined. – sagar Aug 07 '19 at 05:26
  • If you just want to avoid `null` and `undefined`, then explicitly test for those: `const val = a.t; if (val !== null && val !== undefined)` – CertainPerformance Aug 07 '19 at 05:27
  • thanks to point out some good notes. I'll consider them. However, my objects will always store md5 hash strings (upto first 6 characters). So there wouldn't be any possibility of integers or 0. – sagar Aug 07 '19 at 05:45