Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow iteration through a JSONArray in GWT

I'm using GWT to build an application, and I'm facing serious speed issues with something that I thought would be pretty fast. I have a JSONObject with data in the following structure (but that is much larger):

{"nodeData" : [ 
                { "name":"one", "attributes":["uno","dos"]}, 
                {"name":"two", "attributes":["tres"]}
              ]
}

I am trying to iterate through the JSON object to store all the attributes into an arraylist which every node object has, with attribute sizes ranging from 4 to 800.

JSONObject JSONnode = nodeData.get(i).isObject();
Node node = new Node(JSONnode.get("name").toString();
JSONArray attributeArray = JSONnode.get("Attributes").isArray();
int attributeSize = attributeArray.size();


for(int j = 0; k < attributeSize; j++){
    node.attributeArrayList.add(attributeArray.get(j).toString();
}

The for loop I'm executing is taking about a minute, which seems too long, and I'm not sure how to improve it. The minute is in development mode, but I don't know if it would be any faster when I compile it.

like image 927
aelnaiem Avatar asked Aug 25 '11 14:08

aelnaiem


1 Answers

Have you tried using overlays?

GWT Coding Basics - JavaScript Overlay Types

You can create overlay types pretty easily:-

// An overlay type
class Customer extends JavaScriptObject {

  // Overlay types always have protected, zero-arg ctors
  protected Customer() { }

  // Typically, methods on overlay types are JSNI
  public final native String getFirstName() /*-{ return this.FirstName; }-*/;
  public final native String getLastName()  /*-{ return this.LastName;  }-*/;

  // Note, though, that methods aren't required to be JSNI
  public final String getFullName() {
    return getFirstName() + " " + getLastName();
  }
}

Very easy to use and I think would be much faster than using JSONObject objects.

like image 185
mekondelta Avatar answered Nov 04 '22 21:11

mekondelta