memtimintursun / aspjson

Automatically exported from code.google.com/p/aspjson
0 stars 0 forks source link

poor jsEncode performance #11

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
jsEncode average speed: 
IIS5: 10 KB/s to 100KB/s 
IIS7: 10 KB/s to 168KB/s 
(results depend on string size, slower on longer strings)

My proposed code speed:
IIS5: 306 KB/s to 360KB/s
IIS7: 360 KB/s to 580KB/s
(result now depends on characters, faster on ascii, slower on Chinese)

What version of the product are you using? On what operating system?
JSON2.0.2.asp, Tested speed on IIS5 Windows 2000 & IIS7 Windows 2008

proposed code (3x to 50x faster):

Function jsEncode(str)
    Dim i, j, s, l, c, p, a, cv(127), js()
    cv(8)="\b": cv(9)="\t": cv(10)="\n": cv(12)="\f": cv(13)="\r":
cv(34)="\""": cv(47)="\/": cv(92)="\\"
    j = 0: s = 1
    Redim js(len(str))
    For i = 1 To Len(str)
        c = Mid(str, i, 1)
        a = AscW(c)
        If a < 127  Then
            If IsEmpty(cv(a)) Then
                If a > 31 Then
                    l = l + 1
                Else
                    js(j) = Mid(str, s, l) & "\u" & Right("000" & Hex(a),4)
                    l = 0: j = j + 1: s = i + 1
                End If 
            Else
                js(j) = Mid(str, s, l) & cv(a)
                l = 0: j = j + 1: s = i + 1
            End if
        Else
            js(j) = Mid(str, s, l) & "\u" & Right("000" & Hex(a),4)
            l = 0: j = j + 1: s = i + 1
        End If 
    Next
    jsEncode = Join(js,"")
End Function

Actual code for reference:
    Function jsEncode(str)
        Dim i, j, aL1, aL2, c, p

        aL1 = Array(&h22, &h5C, &h2F, &h08, &h0C, &h0A, &h0D, &h09)
        aL2 = Array(&h22, &h5C, &h2F, &h62, &h66, &h6E, &h72, &h74)
        For i = 1 To Len(str)
            p = True
            c = Mid(str, i, 1)
            For j = 0 To 7
                If c = Chr(aL1(j)) Then
                    jsEncode = jsEncode & "\" & Chr(aL2(j))
                    p = False
                    Exit For
                End If
            Next

            If p Then 
                Dim a
                a = AscW(c)
                If a > 31 And a < 127 Then
                    jsEncode = jsEncode & c
                ElseIf a > -1 Or a < 65535 Then
                    jsEncode = jsEncode & "\u" & String(4 - Len(Hex(a)), "0") & Hex(a)
                End If 
            End If
        Next
    End Function

Original issue reported on code.google.com by gvanvrec...@gmail.com on 28 Jul 2009 at 8:43

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Function jsEncod8(str)
    Dim i, j, s, l, c, a, cv(127), js()
    cv(8)="\b": cv(9)="\t": cv(10)="\n": cv(12)="\f": cv(13)="\r": cv(34)="\""":
cv(47)="\/": cv(92)="\\"
    j = 0: s = 1: l=0
    Redim js(len(str))
    For i = 1 To Len(str)
        c = Mid(str, i, 1)
        a = AscW(c)
        If a < 127  Then
            If IsEmpty(cv(a)) Then
                If a > 31 Then
                    l = l + 1
                Else
                    js(j) = Mid(str, s, l) & "\u" & Right("000" & Hex(a),4)
                    l = 0: j = j + 1: s = i + 1
                End If 
            Else
                js(j) = Mid(str, s, l) & cv(a)
                l = 0: j = j + 1: s = i + 1
            End if
        Else
            js(j) = Mid(str, s, l) & "\u" & Right("000" & Hex(a),4)
            l = 0: j = j + 1: s = i + 1
        End If 
    Next
    js(j) = Mid(str, s, l)
    jsEncod8 = Join(js,"") 
End Function

Original comment by gvanvrec...@gmail.com on 28 Jul 2009 at 9:30

GoogleCodeExporter commented 8 years ago
jsEncode(str) has a problem.
It doesn't work with mulitibyte language string as parameter (like 
"尖첨密謐" )

so, I change source code 

    For i = 1 To Len(str)
        c = Mid(str, i, 1)
        a = AscW(c)
        If a < 127  Then

    For i = 1 To Len(str)
        c = Mid(str, i, 1)
        a = AscW(c)
        If a > 31 And a < 127  Then

Original comment by darkma...@gmail.com on 13 Aug 2009 at 2:11

GoogleCodeExporter commented 8 years ago

Original comment by tugrulto...@gmail.com on 8 Nov 2009 at 8:22

GoogleCodeExporter commented 8 years ago

Original comment by tugrulto...@gmail.com on 9 Nov 2009 at 11:04