display | more...

ASP Response Object : Buffer Property

The Buffer property specifies whether to buffer page output. When page output is buffered, the server does not send a response to the client until all of the server scripts on the current page have been processed, or until the Response.Flush or Response.End method has been called.

If the Buffer is set to True, then the server will buffer the output. If it is set to False, then no buffering occurs. The Buffer property can not be set or changed after the server has sent any output to the browser. Therefore, the property should be set on the first line of the ASP page (or at least before any HTML appears, any Response.Writes are used, and any Cookies are set).

The default value of the Buffer property in ASP 2.0 (IIS versions up to and including 4.0) is False. The default value in ASP 3.0 (IIS 5.0 and up) is True1.

Example (VBScript):

  Response.Buffer = True
  Dim count
  For count=0 To 10000
    Response.Write count & "<br>"
Nothing would be written to the browser until the loop had finished counting up to 10000, and then all of the buffered output would be sent at one time.

Placing a call to Response.Flush after every Response.Write call results in a script which functions exactly as if Response.Buffer = False.

Tips & Hints

Buffering to Improve Performance
Buffering minimizes the number of writes to the client's browser, and since each write has overhead (in IIS and in the amount of data sent to the browser), the fewer the writes, the better. TCP/IP works much more efficiently when it can send a few large blocks of data than when it has to send many small blocks. This occurs because of TCP/IP's use of the slow start and Nagling algorithms to minimize network congestion.

Unfortunately buffering, while improving actual performance, can actually cause users to perceive that ASP pages are being less responsive, because they have to wait for the entire page to be generated before seeing any of it. The answer is to strike a balance between buffering and sending (flushing) the buffer with Response.Flush. For example, in the code above, the server did not send any information until it was finished counting up to 10000. This may result in perceived unresponsivness to the end user. A solution to this problem would be to send blocks of information every so often, so that the end user can see progress.

Instead of waiting for the loop to count all the way to 10000 before sending information, we can instead send a block of information after every 1000 increments. To accomplish this I've written a remainder function, since VBScript has no built-in remainder function or operator. It returns 0 only when the dividend is a multiple of the divisor2.

  Response.Buffer = True
  Dim count
  For count=1 To 10000
    Response.Write count & "<br>"
    If Remainder(count, 1000) = 0 Then
    End If

  Function Remainder(dividend, divisor)
    Dim quotient
    If divisor <> 0 Then
      '(\) is used for integer division
      quotient = dividend\divisor
      Remainder = dividend - (quotient * divisor)
      Remainder = 0
    End If
  End Function
This code flushes the buffered output whenever the remainder of the count variable divided by 1000 is equal to 0. That is, only when count is a multiple of 1000 (2000, 3000, etc...).

Back to the ASP Response Object
Back to ASP Objects
1 For Windows 2000, this actually depends on the way you installed Windows 2000. If it is clean install of Windows 2000, then the default value will be True. If the install is an upgrade, then the default value will be False.
2 For my purposes, I've coded the function so that the remainder of any dividend divided by 0 is also 0, instead of undefined. This keeps the script from throwing an error if 0 is used for the divisor.


All code is my own (and is environment-friendly)

Log in or register to write something here or to contact authors.