42

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!

1
  • Can you specify the package under which the JSONObject you are referring to resides ? Mar 26, 2019 at 17:54

5 Answers 5

52

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.

4
  • 10
    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
    Sep 8, 2010 at 22:10
  • @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
    Sep 8, 2010 at 22:48
  • 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
    Sep 9, 2010 at 0:30
  • There might be very little difference in performance but the actual thing is depend on your data and maybe on your choice. I personally use Map<String, Object> response = new HashMap<>(); . Because this is native to core java that's why I prefer it. Otherwise I tried JsonObject from google Gson library as well. Also someone can use POJO. For performance POJO will be better but doesn't fit for general.
    – Noor Khan
    Apr 18, 2022 at 7:09
6

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?
5

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.

1

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.

0

The only performance overhead is on casting data! As you JSONObject stores data on a HashMap of objects and it casts the datatype you want.

Not the answer you're looking for? Browse other questions tagged or ask your own question.