Hypothetical Memory map of Javascript Objects (model only…)

Mart The Martian
2 min readMar 8, 2018

--

In terms of memory, object is an aggregate of primitives. Suppose there are at least three level of things at play here. $0000 is short for addresses of allocated memory. they are stored in pointer or variables to find the child level value.

var obj= { 
a: “hoho”,
b: “momo”,
};

We start with inside:

  1. system allocate memory block for primitive data “hoho” at address $0001{“hoho‘};
  2. system allocate memory block for primitive pointer a at address $000a{“a”, $0001};
  3. … “momo” => $0002{“momo‘};
  4. … b => $000b{“b”,$0002};
  5. aggregate group address in memory: $OBJECT001{“o”,$000a,$000b};
  6. … aggregate variable obj => $0obj{“obj”,$OBJECT001{“o”,$000a,$000b}};

possibly, most likely not accurate, but can be used as analogy.

Example II: o in o

var obj= { 
a: “hoho”,
b: “momo”,
o:{
c:"ccccc",
d:"dddd",
},
e:{},
};

i suppose this should be quite recursive process. recursion keeps looking deep. starts with the deepest? not sure if this is optimal, and surely isn’t how it works in working memory…

  1. primitive value “ccccc”=>$0001{“ccccc”};
  2. primitive “dddd”=>$0002{“dddd”};
  3. primitive pointer ‘c’ => $000c{‘c’,$0001};
  4. primitive pointer ‘d’ => $000d{‘d’,$0002};
  5. primitive pointer “o”=> aggregate group?:: $000o{‘o’,$OBJECT001{$000c,$000d}};
  6. primitive pointer “e” => group?:: $000e{‘e’,$OBJECT002{null}};
  7. “momo”=>$0003{“momo”};
  8. primitive pointer ‘b’ => $000b{‘b’,$0003};
  9. “hoho”=>$0004{“hoho”};
  10. primitive pointer ‘a’ => $000a{‘a’,$0004};
  11. variable obj => aggregate group?:: $0obj{‘obj’,$OBJECT003{$000a,$000b,$000o,$000e}};

that’s all. so there are 3 actual objects here, o, e, and obj.

My model explain this..

References $OBJECT003{$000a,$000b,$000o,$000e} are passed by value, but the primitive inside the objects are kept where they are.

The logic behind this is that any primitive value could be large as GBs, duplicating them is not practical. instead, the address is much smaller, much more predictable.

{}=={} gives false, but obj == object gives true. looks like JS checks the value of these stored references (memory addresses).

--

--