tags:

views:

88

answers:

4

I am wondering about the performance of Java HashMap vs JSONObject.

It seems JSONObject stores data internally using HashMap. But JSONObject might have additional overhead compared to HashMap.

Does any one know about the performance of Java JSONObject compared to HashMap?

Thanks!

+5  A: 

I would say the question doesn't make sense for a few reasons:

  1. Comparing apples to oranges: HashMap and JSONObject are intended for 2 completely different purposes. It's like asking "is the Person class or Company class more efficient for storing a PhoneNumber object". Use what makes sense.
  2. If you are converting to/from JSON, you are likely sending the data to a far away place (like a user's browser). The time taken to send this data over the network and evaluate it in the user's browser will (likely) far eclipse any performance differences of populating a Hashmap or JSONObject.
  3. There is more than 1 "JSONObject" implementation floating around out there.
  4. Finally, you haven't asked about what sort of performance you would like to measure. What are you actually planning to do with these classes?
z5h
+1  A: 

JSONObject does not have too much additional overhead on top of a HashMap. If you are okay with using a HashMap then you should be okay using a JSONObject. This is provided you want to generate JSON.

JSONObject checks for validity of values that you are storing as part of your JSONObject, to make sure it conforms to the JSON spec. For e.g. NaN values do not form a part of valid JSON. Apart from this, JSONObject can generate json strings (regular | prettfied). Those strings can get pretty big, depending on the amount of JSON. Also, JSONObject uses StringBuffer, so one of the many things that i would do would be to replace all occurrences of StringBuffer with StringBuilder.

JSONObject (from org.json) is one of the simple JSON libraries that you can use. If you want something very efficient, use something like Jackson.

Rahul
+4  A: 

As you said, JSONObject is backed by a HashMap.

Because of this, performance will be almost identical. JSONObject.get() adds a null check, and will throw an exception if a key isn't found. JSONObject.put() just calls map.put().

So, there is almost no overhead. If you are dealing with JSON objects, you should always use JSONObject over HashMap.

jjnguy
Actually I disagree with this -- since JSONObject is specific to one library, unlike Maps, exposing it as a type will add more coupling to specific library and its datatypes. So I would not categorically say it should always be used.
StaxMan
@Stax, I can see your point. But, if you need to de-couple a part of the code, you can always extract the data into a map yourself.
jjnguy
True. I guess overall it is more of a question of POJOs vs library-specific abstractions; I like to keep data format aspects closer to edges.
StaxMan
A: 

Existing answers are correct, performance differences between the two are negligible.

Both are basically rather inefficient methods of storing and manipulating data. More efficient method is typically to bind into regular Java objects, which use less memory and are faster to access. Many developers use org.json's simple (primitive) library because it is well-known, but it is possible the least convenient and efficient alternative available. Choices like Jackson and Gson are big improvements so it is worth considering using them.

StaxMan