最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angularjs - Is there a String pool concept in JavaScript? Can we get valueskeys to refer to just one String object? - Stack Over

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Sep 12, 2016 at 11:21 Python BoyPython Boy 1611 gold badge2 silver badges13 bronze badges 4
  • Whats the server side language you are using? If you are using Java, then create a hash map which will reduce your memory usage at client side with faster puting logic. – ngCoder Commented Sep 12, 2016 at 11:26
  • I dont want to load the network with huge response size. Im using Java (Restful WS) – Python Boy Commented Sep 12, 2016 at 11:44
  • Cool ! Then you have the answer go for Hashmap or Treemap which is easy to pute at server level and you will not have duplicate objects. – ngCoder Commented Sep 12, 2016 at 11:47
  • Well to avoid network congestion in the response body we send an array of keys and array of row values, eg. data.keys=['key1', 'key2']; data.values=[['val1', 'val2'], ['val2', 'val4']] and then on client side I map these into associated object ir array of key value pair – Python Boy Commented Sep 12, 2016 at 13:54
Add a ment  | 

2 Answers 2

Reset to default 6

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

JavaScript works similar to Java. It also has a String Pool. When having two equal Strings there is just one memory slot allocated. No new Object is created. This is called Interning.

How JavaScript stores strings

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论