简体   繁体   中英

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

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM