ASP Best Practices

Previous Topic Next Topic

Minimizing Re-Dimensioning of Arrays

When working with arrays, try to avoid the use of the Redim statement. When it is feasible, set the dimension of an array to its maximum size requirement, then leave it at that size. This might not work if memory constraints prevent you from permanently setting a huge array, or several arrays, at maximum. However, every time you Redim an array, your application will take a performance hit, especially when using the Preserve keyword.

In the following example, the Dim and Redim statements are used inefficiently:

<%
  Dim SomeArray ( )
  Redim SomeArray (3)
  SomeArray (0) = "yes"
  SomeArray (1) = "no"
  SomeArray (2) = "maybe"
  .
  .
  .
  'Code executes happily with the current array
  'but then the program needs a bigger array.
  Redim Preserve SomeArray (8)
  SomeArray (3) = …
  .
  .
  .
%>

If the array had been dimensioned at 8 to begin with, some memory would have been wasted, but there would have been a gain in speed.


© 1997-1999 Microsoft Corporation. All rights reserved.