2

I have a question on how objects/arrays are searched. Take the following for example:

while ($obj = $sql->fetch_object()) {
    array_push($jsonObj,array('serial' => $obj->serialNum,'model' => $obj->model,'os' => $obj->os,'man' => $obj->manufacturer,'team' => $obj->team,'enabled'=>'true'));

if($.inArray((JsonObj[i].os)) >= 0){...}

I basically have a multi-dimensional array with 6 "columns" holding computer information.

Would this 'if' take longer to search the object/array if the object also held more details about the computer (ram, harddrive, etc) and had more "columns"? Or does the dot notation prevent that?

2
  • 1
    Until there's a truly huge number of properties, property access is efficient and not really a function of how many there are.
    – Pointy
    Oct 14, 2013 at 16:06
  • Not sure what you're doing there, $.inArray expects 2 arguments. Also it would be easier if you could post the JSON itself, not the PHP code that generated it.
    – Bergi
    Oct 14, 2013 at 16:07

1 Answer 1

0

Property lookup is slower the more you go down the prototype chain. In your case, you are not looping through objects, but rather through array indexes. So once you have the object reference cached, retrieving properties should not be slow.

Avoid looping through object properties when you are already a long way down the chain:

for( property in object.property1.property2.property3[i] ) as this would be indeed slower.

2
  • What do property lookups have to do with the scope chain?
    – Bergi
    Oct 14, 2013 at 18:16
  • Just to be clear, the number of properties in the array doesn't affect the time spent?
    – Tyler H
    Oct 14, 2013 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.