7

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.

Python Boy
  • 161
  • 1
  • 2
  • 12
  • 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 computing logic. – ngCoder Sep 12 '16 at 11:26
  • I dont want to load the network with huge response size. Im using Java (Restful WS) – Python Boy Sep 12 '16 at 11:44
  • Cool ! Then you have the answer go for Hashmap or Treemap which is easy to compute at server level and you will not have duplicate objects. – ngCoder Sep 12 '16 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 Sep 12 '16 at 13:54

1 Answers1

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

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875