For my own curiosity I decided to benchmark the loading speeds of the old framework JSONItem class, compared to the new framework xojo.Data.ParseJSON method, to determine if there are any significant speed differences between them.
Code used for testing…
OLD FRAMEWORK:
Dim jsonObject As JSONItem
Dim i As Integer
for i = 1 to 100000
jsonObject = new JSONItem("{""abc"":1,""def"":2,""ccc"":[1, 2, 3, 4, 5, 6, 7, 8, 9]}")
next i
MsgBox jsonObject.Value("abc")
NEW FRAMEWORK:
Dim jsonObject As xojo.Core.Dictionary
Dim i As Integer
Dim num As Integer
for i = 1 to 100000
jsonObject = xojo.Core.Dictionary(xojo.Data.ParseJSON("{""abc"":1,""def"":2,""ccc"":[1, 2, 3, 4, 5, 6, 7, 8, 9]}"))
next i
num = jsonObject.Value("abc")
MsgBox num.ToText()
I’m still a novice when it comes to the new framework, and aren’t sure if there are perhaps ways to improve the code I used?
RESULTS:
The new framework is the clear winner, in terms of parsing speed.
Now try generating a large json object.
Dang, the new generation method is fast.
I used the code below to generate an “old” JSONItem object, as well as a matching “new” xojo.Core.Dictionary object. The object initialization code was separated from the speed testing code as to not interfere with the profiling speeds. These two test objects are pretty decent in size, and produces the exact same JSON result string.
CREATE TEST OBJECTS:
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim arrInt() As Integer
Dim arrText() As Text
Dim arrStr() As String
Dim tmpJSON As JSONItem
for i = 1 to 1000
arrInt.Append i
arrText.Append i.ToText()
arrStr.Append Str(i)
next i
// old JSON object
oldJSONObject = new JSONItem()
oldJSONObject.Value("abc") = 1
oldJSONObject.Value("def") = "qwe"
for i = 1 to 10
tmpJSON = new JSONItem("[]")
for k = 0 to arrInt.Ubound
tmpJSON.Append arrInt(k)
next k
oldJSONObject.Value("arri" + i.ToText) = tmpJSON
next i
for i = 1 to 10
tmpJSON = new JSONItem("[]")
for k = 0 to arrStr.Ubound
tmpJSON.Append arrStr(k)
next k
oldJSONObject.Value("arrt" + i.ToText) = tmpJSON
next i
oldJSONObject.Value("yui") = "yui"
// new JSON object
newJSONObject = new xojo.Core.Dictionary()
newJSONObject.Value("abc") = 1
newJSONObject.Value("def") = "qwe"
for i = 1 to 10
newJSONObject.Value("arri" + i.ToText) = arrInt
next i
for i = 1 to 10
newJSONObject.Value("arrt" + i.ToText) = arrText
next i
newJSONObject.Value("yui") = "yui"
msgbox "done"
The following methods were used to test the speed of generating JSON strings from the two test objects:
OLD FRAMEWORK:
Dim i As Integer
Dim jsonStr As String
// test generation speed
for i = 1 to 10
jsonStr = oldJSONObject.ToString()
next i
NEW FRAMEWORK:
Dim i As Integer
Dim jsonText As Text
// test generation speed
for i = 1 to 10
jsonText = xojo.Data.GenerateJSON(newJSONObject)
next i
… and the results…
This is really promising and I look forward to utilizing it more.