Javascript classes, compiler performance and memory allocation on new Klass() -


edit: ======================================================== please see revision 13 of test answers:  http://jsperf.com/closure-prototype-static-performance/ ======================================================== 

i prefer declare klasses this, not big fan of object literal way:

first way: private scoping

    function employees() {             var persons = []; // [person, person]              this.isemployed = function(_firstname, _lastname) {                     ( var = 0; < persons.length ; i++ ) {                             if ( persons[i].equals(_firstname, _lastname) ) {                                     return true;                             }                     }                     return false;             }     }      function person(_firstname, _lastname) {             this.equals = function(firstname, lastname) {                     return _firstname == firstname && _lastname == lastname;             }     } 

second way: prototype

alternative use prototype wouldn't access "private" properties such var persons = [] , arguments in person(...) 1 might able live making them public, although person 2 new properties introduced:

    function employees() {             this.persons = []; // [person, person]     }     employees.prototype.isemployed = function(_firstname, _lastname) {             ( var = 0; < this.persons.length ; i++ ) {                     if ( this.persons[i].equals(_firstname, _lastname) ) {                             return true;                     }             }             return false;     };      function person(_firstname, _lastname) {             this.firstname = _firstname;       // 2 new properties. these or function declared more expensive?             this.lastname  = _lastname;     }     person.prototype.equals = function(firstname, lastname) {             return this.firstname == firstname && this.lastname == lastname;     }; 

third way : static approach

another alternative prototype stuff instead make methods outside "static":

    function employees() {             this.persons = []; // [person, person]     }     function isemployed(employee, _firstname, _lastname) {             ( var = 0; < employee.persons.length ; i++ ) {                     if ( equals(employee.persons[i], _firstname, _lastname) ) {                             return true;                     }             }             return false;     }     function person(_firstname, _lastname) {             this.firstname = _firstname;       // 2 new properties. these or function declared more expensive?             this.lastname  = _lastname;     }     function equals(person, firstname, lastname) {             return person.firstname == firstname && person.lastname == lastname;     } 

questions:

1. 1 of these approaches best performance, memory wise , cpu power, , there reason why or when 1 should try avoid or follow other?

2. last example "static" scoping better prototype approach in perspective?

3. , when comes first way, prefered way, because more neat, allows "private" scoping, , code fits nicely in, compiler maybe prototype approach you, or these functions declared on , on again each new person do?

i try avoid using first approach when new() alot , when there alot of methods involved, resort static approach, although want know if have real reaoson this?

4. noticed example didn't include "private" function, , how treated compiler?

example:

    function person(_firstname, _lastname) {             function privatefunction() {                     // how compile treat private functions. created once per new person() ? cheap or expensive?             }     } 

which 1 of these approaches best performance, memory wise , cpu power?

see javascript prototype operator performance: saves memory, faster? , defining methods via prototype vs using in constructor - performance difference?. prototypes save memory.

is there reason why or when 1 should try avoid or follow it?

you'd use privileged methods , scoped variables if care privacy.

is last example "static" scoping better prototype approach?

the lookup might little cheaper, don't care that. putting instance methods on instances (or prototypes) cleaner , avoids global scope pollution.

does compiler maybe prototype approach you, or these functions declared on , on again each new person do?

of course it's optimized interpreter (or @ least modern ones). executable code shared, keep specified semantics different function objects different scope pointers created.

i try avoid using first approach when new() alot , when there alot of methods involved, resort static approach.

you can combine them of course. not of many methods need access private variables, can put them on prototype.

i noticed example didnt include "private" function

yes did. employed , equals function expression private-scoped function declaration.

are created once per new person()? cheap or expensive?

of course - once every different scope. not expensive, if made them static (put them out of constructor scope) cheaper.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -