Should javascript share features whenever possible or create new ones?

I would like to know which situation has more “overhead”:

1) Case 1: 5 million objects sharing 30 functions. every time a function is called, there is overhead because it is necessary to do f.call (instance, arg1, arg2, etc.)

 //example code
function makeObject()
{
  return { method1:func1,
           method2:func2,
           ...
           method30:func30 };
}

2) Case 2: 5 million objects with 30 functions each (= 150 million instances of individual functions). Every time a function is called, there is no “routing-overhead”, but, of course, with the victim having more instances

//example code
function makeObject()
{
  return { method1:func1.bind(asd),
           method2:func2.bind(asd),
           ...
           method30:func30.bind(asd) };
}

5 million is just the number of my fingers printed at a time when my brain is still calculating a good number for an example.

, , , ?

( , eval )

+3
3

prototype- scopeschain, .

, , hash lookup table, javascript / out of scope. , scope chain lookup, --.

lookup , - eval 'ed. eval , ( ).

5m Javascript-, , . 5 , n , " script".

parsing time 150 .

+5

, 2 , :

function makeObject()
{
  return { method1:func1,
           method2:func2,
           ...
           method30:func30 };
}

5 30 . , "150 ". 30.

5 , - . 3-10 , . , , __proto__.

: 5 /30 1 . , . . (, ) /.

+1

?

function myObject() { }

myObject.prototype.method1 = func1
myObject.prototype.method2 = func2,
           ...
myObject.prototype.method30 = func30

var obj = new myObject();

:

obj.method1(); 
obj.method2(); 

Without the need for call()other magic ...

In this case, you will not need to fill each instance of your object with the same 30 properties.

+1
source

All Articles