Copies of memory in the case

function myClass()
{
  //lots and lots of vars and code here.

   this.bar = function()
   {
     //do something with the numerous enclosed myClass vars
   }

   this.foo = function()
   {
      alert('Hello'); //don't do anything with the enclosed variables.
   }
}

Each instance of myClass gets its own copy of bar and foo, so prototype methods use less memory. However, I would like to learn more about using internal memory methods.

It seems obvious to me that (1) below should be true. Do you agree?

  • Not only do individual instances of myClass have their own separate columns, but they must also have their own separate copies of the myClass shell. (or how does the bar method store myClass variables directly based on each instance?)

Now (2) the question arises that I am really worried about.

  • Since the foo internal method does not use anything in the myClass wrapper, that is, the natural qustion: Is javascript smart enough not to store the myClass wrapper in memory to use foo?
+3
3

, . , - , .

, , bar, foo " " . , . , , " " , - .

, , JavaScript. .

, .

:

10.4.3 , , F, thisArg, :

  • , ThisBinding thisArg.
  • thisArg null undefined, ThisBinding .
  • (thisArg) , ThisBinding ToObject (thisArg).
  • Else ThisBinding Arg.
  • localEnv NewDeclarativeEnvironment [[]] F .
  • LexicalEnvironment localEnv.
  • localEnv.

NewDeclarativeEnvironment.

NewDeclarativeEnvironment

10.2.2.2 NewDeclarativeEnvironment (E) NewDeclarativeEnvironment , null E :

  • env .
  • envRec - , .
  • envs envRec.
  • env E.
  • env.

E, [[Scope]] Object

13.2 . FormalParameterList, FunctionBody, , , Boolean flag Strict, Function :

  • ECMAScript F .
  • , [[Get]], F, 8.12.
  • [[Class]] F "Function".
  • [[Prototype]] F , 15.3.3.1.
  • [[Get]] F, 15.3.5.4.
  • [[Call]] F, 13.2.1.
  • [[Construct]] F, 13.2.2.
  • [[HasInstance]] F, 15.3.5.3.
  • [[]] F Scope.
  • , .

, , . . , , "" .

, , .

:

. () , . , , . . , Declaration FunctionDeclarations, .

Lexical Environment

+1

, . , . , , , V8 (chrome JS engine) Spidermonkey (Firefox) JScript (IE) .....

( , GHC ), ( ) , "" (bar), , , JS MyClass, , , this.bar, , foo , , MyClass, .

, JS eval, , eval, , JS .

+1

, , , : -)

  • Spidermonkey: - . , .

  • V8: - , " ". " ", , , , V8 GC .

  • Unfortunately, these meager links are all I can find. A direct analysis of the engine source code is likely to require the addition of more meaningful input.

So, yes, different engines implement different optimization strategies when they can.

+1
source

Source: https://habr.com/ru/post/1791762/


All Articles