0

I have a variable that is being set based on if it an object has a length as one of its properties. If not then I need to check if one of the properties exists. If so then get the first array stored in that object and return the length, else the variable is set to one. This could be combined all in statement like this.

var b=someObject;
var len = b.num||(b.arrs?(function(a){for(var x in a)return a[x].length})(b.arrs):false)||1;

The other way of doing this is pulling out the function and doing it like this

function getLength(a){for(var x in a)return a[x].length})
var b=someObject;
var len = b.num||(b.arrs?getLength(b.arrs):false)||1;

I was wondering if there is a performance penalty for doing it one way compared to the other? Is there a difference in how the javascript engine looks at them? The reason I would prefer the first way is it keeps me from having a bunch of extra helper functions.

qw3n
  • 6,236
  • 6
  • 33
  • 62
  • Assuming that there is a performance difference either way: will it be large enough that the user can notice it? Doesn't sound likely. For the academic part of the question: http://jsperf.com – Jon Sep 29 '12 at 18:53
  • 1
    There will hardly be a performance difference, but readability will certainly decrease. Although, even the second piece is quite obscure. Btw. it looks to me that your code is incorrect since you have the `return` statement inside the `for` loop, but maybe that's what you want. See, it's not clear what the code is supposed to do. – Felix Kling Sep 29 '12 at 18:53
  • I don't get the `getLength` function. What's the purpose? – elclanrs Sep 29 '12 at 18:54
  • @elclanrs it just returns the length of an array stored in object. – qw3n Sep 29 '12 at 18:57
  • Possible dublication: http://stackoverflow.com/questions/80802/does-use-of-anonymous-functions-affect-performance – Krycke Sep 29 '12 at 18:57
  • Then I don't think the `for` loop is necessary if you know the object key you want to access... – elclanrs Sep 29 '12 at 18:58
  • @elclanrs that's the problem I don't know the key. All I know is that it will be an object holding 1 or more arrays of the same length. – qw3n Sep 29 '12 at 19:00
  • So you want to get the max length, is that it? That loop just looks wrong for some reason. – elclanrs Sep 29 '12 at 19:01
  • @Krycke This is a duplication of that question. I looked but all my searches kept coming up with questions about anonymous versus normal for creating objects. – qw3n Sep 29 '12 at 19:02
  • @elclanrs the loop is weird because it was the only way I knew how get what I wanted. I have an object containing 1 or more arrays, but each array should be the same length. I also don't know the keys. What I need is, if the object exists, the length of one of the arrays (it doesn't matter which one since they should all be the same length). – qw3n Sep 29 '12 at 19:06

1 Answers1

1

The anonymous inline version is executed and discarded. Whereas the separate function instance is

a) Kept in memory and popped out with the rest of the local variables, if it is declared within a function

b) Kept in memory for the life time of the page if it is defined in global space as a member of the window object.

So if you are unlikely to need it again, it is best to go the anonymous inline route i believe.

techfoobar
  • 65,616
  • 14
  • 114
  • 135