javascript - Best (most performant) way to declare (class) properties with unknown values in v8 -


so learned bit hidden class concept in v8. said should declare properties in constructor (if using prototype based "pseudo classes") , should not delete them or add new ones outside of constructor. far, good.

1) properties know type (that shouldn't change) not (initial) value?

for example, sufficient this:

var foo = function () {     this.mystring;     this.mynumber; } 

... , assign concrete values later on, or better assign "bogus" value upfront, this:

var foo = function () {     this.mystring = "";     this.mynumber = 0; } 

2) thing objects. know object wont have fixed structure, want use hash map. there (non verbose) way tell compiler want use way, isn't optimized (and deopted later on)?

update

thanks input! after reading comments (and more on internet) consider these points "best practices":

  • do define all properties of class in constructor (also applies defining simple objects)
  • you have assign something these properties, if thats null or undefined - stating this.mystring; apparently not enough
  • because have assign anyways think assigning "bogus" value in case can't assign final value immediatly cannot hurt, compiler "know" asap type want use. so, example this.mystring = "";
  • in case of objects, assign whole structure if know beforehand, , again assign dummy values it's properties if don't know them immediatly. otherwise, example when intending use object hashmap, do: this.myobject = {};. think not worth indicating compiler should hashmap. if really want this, found trick assigns dummy property object , deletes immediatly afterwards. won't this.
  • as smaller arrays it's apparently recommended (reference: https://www.youtube.com/watch?v=ujpdhx5ztaw&feature=youtu.be&t=25m40s) preallocate them if know final size, example: this.myarray = new array(4);
  • don't delete properties later on! null them if needed
  • don't change types after assigning! add hidden class , hurt performance. think thats best practice anyways. case have different types function arguments anyways. in case convert them same target type.
  • same applies if keep adding additional properties later on.

that being said, think doing lean cleaner , more organized code, , helps documenting.

yeah, 1 little thing unsure remains: if define properties in function (for example kind of configure() method) called within constructor?

re 1): reading properties, in first snippet, not object. need assign them create properties.

but object properties doesn't matter values initialise them with, long initialise them. undefined should fine.

the concrete values more relevant arrays, want make sure create them right elements (and without holes!) because vm tries keep them homogeneous. in particular, never use array constructor, because creates holes.

re 2): there ways trick vm using dictionary representation, depend on vm , version , aren't reliable. in general, best avoid using objects maps altogether. since es6, there proper map class.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -