0

I'm building string-dense character descriptions, using the following character object (below). Efficiency is pretty important, and I know that arrays with long strings get top-heavy fast. Assuming that, in both cases, properties can be referenced with similar ease, and that the values are identical, are there any major differences between the following two objects, in terms of efficiency?

this.genphysdesc = genphysdesc;
this.facetype = facetype;
this.bodytype = bodytype;
this.haircol = haircol;
this.gender = gender;
this.pronA = pronA;
this.pronB = pronB;
this.pronC = pronC;
this.pronD = pronD;

VS.

this.physdesc = [genphysdesc, facetype, bodytype, haircol]
this.gender = [gender, pronA, pronB, pronC, pronD]

In either event, values will be strings of ~5-35 characters each.

Let me know if any of this is unclear. Thanks for your help!

corvidia
  • 11
  • 3
  • 1
    Test it. This sort of performance optimisation falls squarely in the "premature optimisation" camp. Even if there is a difference, it's likely to be markedly different in different browsers, and the inter–browser difference is likely more than between object structures. Just do it however seems best and address performance if there's an issue later. – RobG Mar 07 '16 at 01:22
  • @RobG Thanks for the feedback! I should have mentioned that I'm converting a large number of arrays into objects for the sake of rationalization and a slight but noticeable performance boost. What I've tested so far seems solid, but I wanted to verify with someone more experienced than myself before getting too far down the line & winding up in an unforeseen bind. I'm still rather new to JS, so the tip about inter-browser performance variation is great information to have. I take it from your answer that you don't see any obvious reason to prefer one method over the other? Much appreciated. – corvidia Mar 07 '16 at 07:05

1 Answers1

0

Your first case used objects while the second used arrays.

Assuming you want the differences between arrays:

// Hoisting section
var a = [];
var propA, propB, propC, propD, propE, propF;
propA = 1;
propB = 2;
propC = 3;
propD = 4;
propE = 5;
propF = 6;



a.push( propA );
a.push( propB );
a.push( propC );
a.push( propD );
a.push( propE );
a.push( propF );

// vs.

a = [ propA, propB, propC, propD, propE, propF ];

console.log( a );

Second version only assigns once so its faster. Object assignment would be the same with different syntax.

Take a look at hoisting:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

There are some performance differences and benchmarks for [] vs. push syntax: Is there a reason JavaScript developers don't use Array.push()?

Community
  • 1
  • 1
Vladimir Ramik
  • 1,920
  • 2
  • 13
  • 23
  • I was highlighting the difference asked but have edited my answer to highlight hoisting which I linked. – Vladimir Ramik Mar 07 '16 at 01:38
  • Thanks for the quick response! Up until now I have been using arrays almost exclusively, no pushing, for exactly this reason - performance. I found it was running rather sluggishly, probably due to the amount of cross-referencing (?). Because of this and also for the sake of improved organization, I've started consolidating items into objects. I was just wondering, before getting too far along in this conversion process, if there is any benefit to having more individual properties vs. consolidating attributes within properties as arrays. Thanks for the link. – corvidia Mar 07 '16 at 06:57