ReplaceAll too slow

Is there a better way to write this code. It works fine but is extremely slow when processing large sets of data.

Function removeIllegalCharacters(name as string)

dim arrIllegal() as string = array ("\","/",":","?","<",">","*","“","&","@","$","|","%","~")
dim arrReplace() as string = array ("92","47","58","63","60","62","42","34","38","64","36","124","37","126")

dim i as integer
For i = 0 To 13
  name = ReplaceAll(name,arrIllegal(i),"-chr" + arrReplace(i) + "-")
Next

return name

I have a similar function to put the same characters back (also too slow).

How “slow” is it.
How many characters is your source string?

Try to use array.indexOf and the re-assign the indexed value

The function is called about 2000 times in one operation, each processing a block of text average length 15 characters.

I ran a test using this test string:

var s as string = String.FromArray(array( "\","/",":","?","<",">","*","“","&","@","$","|","%","~", " ") )
s = s.Repeat_MTC( 1000 )

That takes about 13 ms.

Modifying the code like this reduces that to about 3 ms.

Public Function RemoveIllegalCharacters(name As String) As String
  static arrIllegal() as string
  static arrReplace() as string = array ("92","47","58","63","60","62","42","34","38","64","36","124","37","126")
  
  if arrIllegal.Count = 0 then
    arrIllegal = array ("\","/",":","?","<",">","*","“","&","@","$","|","%","~")
    for i as integer = 0 to arrReplace.LastIndex
      arrReplace( i ) = "-chr" + arrReplace( i ) + "-"
    next
  end if
  
  dim i as integer
  For i = 0 To arrIllegal.LastIndex
    name = name.ReplaceAllBytes(arrIllegal(i), arrReplace(i))
  Next
  
  return name
End Function
2 Likes

Finally, instead of trying to roll your own, you could use EncodeURLComponent, and that takes about 0.5 ms for the above test.

3 Likes

Or about 0.15 ms after the first usage.

2 Likes