Skip to content

unnessary vba.Len() calls #292

Description

@kausarkhanlp504-2

your are calling "vba.Len()" function at every iteration of while loop in 'Json_SkipSpaces' Sub Procedure.
imagine if json string length is 1M+ Characters. then it could slow down the performance significantly. i added new variable to the
ParseJson() function -> json_Length

Public Function ParseJson(ByVal JsonString As String) As Object
    ' Remove vbCr, vbLf, and vbTab from json_String
    JsonString = VBA.Replace(VBA.Replace(VBA.Replace(JsonString, VBA.vbCr, ""), VBA.vbLf, ""), VBA.vbTab, "")
    
    Dim json_Index As Long, json_Length As Long
    json_Index = 1
    json_Length = Len(JsonString)

    json_SkipSpaces JsonString, json_Index, json_Length
    Select Case VBA.Mid$(JsonString, json_Index, 1)
    Case "{"
        Set ParseJson = json_ParseObject(JsonString, json_Index, json_Length)
    Case "["
        Set ParseJson = json_ParseArray(JsonString, json_Index, json_Length)
    Case Else
        ' Error: Invalid JSON string
        Err.Raise 10001, "JSONConverter", json_ParseErrorMessage(JsonString, json_Index, "Expecting '{' or '['")
    End Select
End Function

and then pass this variable to json_Skipspaces procedure and to other procedures where it needed.
this improve the performance significantly

old:

Private Sub json_SkipSpaces(json_String As String, ByRef json_Index As Long)
    ' Increment index to skip over spaces
    Do While json_index > 0 And json_Index <= VBA.Len(json_String) And VBA.Mid$(json_String, json_Index, 1) = " "
        json_Index = json_Index + 1
    Loop
End Sub

new:

Private Sub json_SkipSpaces(json_String As String, ByRef json_Index As Long, ByRef json_Length As Long)
    ' Increment index to skip over spaces
    Do While json_Index <= json_Length And VBA.Mid$(json_String, json_Index, 1) = " "
        json_Index = json_Index + 1
    Loop
End Sub

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions