Differences in JavaScript Static Object Creation Approaches -


i'm java developer trying learn javascript ethos.

are these 2 approaches creating static object in javascript different in non-syntactical way? approach more acceptable javascript programmer's perspective?

approach a:

var obj = {}; obj.field1 = /* expression */; obj.field2 = /* expression */; 

approach b:

var obj = {     field1: /* expression */,     field2: /* expression */ }; 

i believe both of objects end being created in similar manner, , difference in performance (if any) minute point shouldn't notice difference.

this not universal, though. when creating arrays, more efficient use similar approach b. because (at least v8) allocating array set size more efficient because knows size of array.

// here v8 can see want 4-element array containing numbers: var = [1, 2, 3, 4];  // don't this: = []; // here v8 knows nothing array for(var = 1; <= 4; i++) {     a.push(i); } 

source: smashing magazine

i suggest trying out chrome profiling tools see if can notice difference between efficiency of both approaches, although think if there difference small make difference.

further more, if new javascript, suggest checking out awesome javascript style guide created airbnb. there ton of great tips writing efficient , clean javascript.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -