Search code examples
.netperformancestringbuilderreadability

Does whitespace matter when using StringBuilder to generate HTML server side?


I'm wondering if using whitespaces to represent indentation (for readability) has any negative effect on performance when using StringBuilder to generate HTML.

Taking this simplified example:

 str.AppendLine("<div>")
 str.AppendLine("    <span>Test</span>")
 str.AppendLine("</div>")

As opposed to:

 str.AppendLine("<div>")
 str.AppendLine("<span>Test</span>")
 str.AppendLine("</div>")

I'm looking for as detailed of an explanation as possible as to why or why not this is the case


Solution

  • The answer is: it depends. There will be an impact on performance if you are doing a large number of operations with a lot of whitespace compared with the non-whitespace counterpart. Here is a benchmark test:

    Dim t1 = Task.Run(New Func(Of TimeSpan)(
                              Function()
                                  Dim start = Now
                                  Dim sb As New System.Text.StringBuilder
    
                                  For i = 1 To 5000000
                                      sb.AppendLine("                                   My Value")
                                  Next
    
                                  sb.ToString()
                                  Return Now - start
                              End Function))
    
    Dim t2 = Task.Run(New Func(Of TimeSpan)(
                              Function()
                                  Dim start = Now
                                  Dim sb As New System.Text.StringBuilder
    
                                  For i = 1 To 5000000
                                      sb.AppendLine("My Value")
                                  Next
    
                                  sb.ToString()
                                  Return Now - start
                              End Function))
    
    Debug.Print(String.Format("With Whitespace (ms): " & t1.Result.Milliseconds))
    Debug.Print(String.Format("Without Whitespace (ms): " & t2.Result.Milliseconds))
    

    This is an extreme example where each line has a lot of extra whitespace compared with the non-whitespace, and it builds five million lines. In this example, the output is:

    With Whitespace (ms): 757

    Without Whitespace (ms): 371

    However, if you reduce the number of lines built by half and reduce the amount of additional whitespace per line by half, the output becomes:

    With Whitespace (ms): 223

    Without Whitespace (ms): 175

    And then reduce number of lines and whitespace again by half again (i.e. now a quarter of the original):

    With Whitespace (ms): 87

    Without Whitespace (ms): 66

    So the point is, it's true, it will have an impact, but it depends on how you're using it, and in the end, it's only a difference of a few hundred milliseconds in extreme cases. Ultimately, it's up to you (and your peers) to determine what's important in each situation: readability for a programmer, or responsiveness for the user.