How is memory handled by javascript objects?

Given:

Say that EventXObj is a javascript object that has many attributes and some methods.

The XObj event can be associated with a simple line identifier as follows.

[ "id1" : Event1Obj , "id2": Event2Obj, ..., "id1000": Event1000Obj ] 

And we want to imagine how one PlaceXObject can contain many events.

QUESTION:

As for below, does script A make much more memory than script B?

Scenario A

 PlaceX.events = [ "id1", "id2", "id75", ... ] 

Scenario b

 PlaceX.events = [ Event1Obj, Event2Obj, Event75Obj, ... ] 
+4
source share
2 answers

Scenario B is probably the most memory efficient from a general point of view. Everything in JavaScript is like a pointer / link, so B uses one (or so) integer for every event contained in your array.

Scenario A uses as much memory as your lines, so for a long line this would be bad. For your case, with very short lines, there is not much difference.

+2
source

It depends entirely on the memory model used by your JavaScript engine.

Taking the current version of V8 as an example (suppose 32 bits):

  • Pointers = 4 bytes.
  • Lines = 12 + [line length].
  • Concatenated strings = 12 + 2 * 4 bytes (2 * 4, because this is a pair of pointers to existing strings). However, if you want, you can force the formatting of strings.
  • Dictionary with n elements = 3 * 4 * 2 * nearest PowerOfTwo (n) bytes.

Now you can do the math for the current version of V8.

+2
source

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


All Articles