Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a String pool concept in JavaScript? Can we get values/keys to refer to just one String object?

Tags:

I have a large json map with around 1 million objects and each object with around 200 key-value pair. eg. [{key1 : val1, key2 : val2, ...}, {key1 : val3, key2 : val4, ...}]

as you see the keys are getting duplicated here, with each key means a new String object. Is there any alternative way where I can say that all duplicate keys should point to same String object to reduce the memory size of map. With the mentioned stats the browser blows up with more than 1Gb of memory.

like image 583
Python Boy Avatar asked Sep 12 '16 11:09

Python Boy


1 Answers

as you see the keys are getting duplicated here, with each key means a new String object.

Well, no, they each get a string primitive. Granted it a subtle distinction, but JavaScript has both:

var sp = "primitive";
var so = new String("object");

Is there a String pool concept in JavaScript?

Not in terms of anything external that you can intentionally invoke such as Java's intern.

A given JavaScript engine (V8, SpiderMonkey, etc.) may or may not reuse string primitives under the covers as an optimization; it can because strings are immutable in JavaScript, but whether it's ever made it to the top of a development priority list...

like image 166
T.J. Crowder Avatar answered Sep 26 '22 16:09

T.J. Crowder