0

Method A (conventional method):

  1. We get JsonArray from an API
  2. Iterate that JsonArray (inside for loop) and Pass values from JsonArray to an object of Model class (Getter and Setter) and add that object of the model class to an ArrayList
  3. Pass that ArrayList to recyclerAdapter
  4. Iterate that ArrayList inside the onBindViewHolder method of recyclerAdapter
  5. and set values to textView, imageView etc inside the onBindViewHolder method

Method B:

  1. We get JsonArray from an API
  2. Pass the whole JsonArray to recyclerAdapter
  3. Iterate jsonArray inside onBindViewHolder (without using ArrayList) and setValues directly to textViews, imageViews etc as shown below

    @Override
    public void onBindViewHolder(Holder holder, int i) {
    
        try {
        holder.textView.setText(jsonArray.getJSONObject(i).getString("textKey"));
        } catch (JSONException e) {
        e.printStackTrace();
        }
    }
    

Will, there be any performance improvement while I use the second method as we are not using model class and ArrayList.

Will the performance decrease or remain same.

I don't care about readability, my only concern here is performance.

Note:- using try catch multiple times has no effect on performance so we can ignore that.

Mukul Bhardwaj
  • 562
  • 5
  • 16

1 Answers1

2

Will, there be any performance improvement while I use the second method as we are not using model class and ArrayList.

Will the performance decrease or remain same.

I don't care about readability, my only concern here is performance.

You will get a performance decrease with method B because you have extra call whenever you're calling the following:

holder.textView.setText(jsonArray.getJSONObject(i).getString("textKey"));

if you break down the above line, it would be something like this:

  • holder.textView.setText()
  • jsonArray.getJSONObject(i)
  • JSONObject.getString("textKey")

You should note that JSONObject.getString("textKey") need to do something heavy to find the value with the key textKey by calling the following getString method code:

public String getString(String name) throws JSONException {
    Object object = get(name);
    String result = JSON.toString(object);
    if (result == null) {
        throw JSON.typeMismatch(name, object, "String");
    }
    return result;
}

you can see there is another extra method call of JSON object with JSON.toString(object):

static String toString(Object value) {
    if (value instanceof String) {
        return (String) value;
    } else if (value != null) {
        return String.valueOf(value);
    }
    return null;
}

You will get a performance hit because of this (although negligible).

The worse part is the following line:

holder.textView.setText(jsonArray.getJSONObject(i).getString("textKey"));

will always be called whenever your View is recycled. So, the performance hit is multiplied.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • Pretty clean breakdown of the functionality of JSON in java. But we are also using get(i) method with ArrayList. And further, if the ArrayList is of the type of Model class it has to do the same. like holder.textView.setText(arrayList.get(i).gettext()); Is this method faster than what you explained above. – Mukul Bhardwaj Mar 28 '19 at 07:20
  • Yes, you need to use get(i). But `ArrayList.get(i)` is only getting an item from an array by using the `i` as the index which is fast. Take a look the source code at https://android.googlesource.com/platform/libcore/+/android-6.0.1_r77/luni/src/main/java/java/util/ArrayList.java#306 – ישו אוהב אותך Mar 28 '19 at 07:26