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):
{"nodeD开发者_如何学Pythonata" : [
{ "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.
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.
How do you use GWT ? Inside an IDE ? In my experience, having too many breakpoints slows down the execution flow, may be you could check that ? Particularly when I see that in production it seems fine...
If everything else fails, you can always write that in native Javascript and call it via JSNI.
精彩评论