KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Avoid Building Long Text in Text Variables
PRODUCT: 4D | VERSION: 21 | PLATFORM: Mac & Win
Published On: March 20, 2026
Text variables are convenient for storing strings, but they are not efficient for building large, continuously growing reports. As the size of a text variable increases, operations that append or manipulate its contents become progressively slower. For long or continuously generated output, it is better to offload the data to either a temporary on‑disk document or a BLOB.

In the following example, the report is built by repeatedly appending new text to a Text variable. As the variable grows, each append operation becomes slower:
var $myReport : Text
var $newTextData : Text
var $buidldingReport : Boolean
While ($buildingReport)
   //...
   $myReport:=$myRepor+$newTextData
   //...
End while


If the final output will be exported to a file, writing directly to a temporary document avoids the performance cost of large in‑memory text operations. Commands such as Open document, SEND PACKET, and CLOSE DOCUMENT can be used:
var $vhDoc : Time
$vhDoc:=Open document("Temporary.txt"; Read and write) //Open a document
If (OK=1)
   While ($buildingReport)
     // ...
     SEND PACKET($vhDoc; $newTextData)
     // ...
   End while
   CLOSE DOCUMENT($vhDoc) //Close the document
End if


For cases where you want to avoid managing on‑disk files, a BLOB provides a faster alternative. Appending text to a BLOB using TEXT TO BLOB is significantly more efficient than repeatedly appending to a growing text variable:
var $myBlob : Blob
While ($buildingReport)
   //...
   TEXT TO BLOB($newData; $newTextData; UTF8 text without length; *)
   //...
End while