Formula execution limits
You can use any valid JavaScript code in your formulas, including arrays, for loops, etc. However, to protect our computational resources, the total execution time of any formula is limited to 3000 ms. If server-side script takes too long to execute, it will be aborted by the system. The length of a parsed server-side script cannot exceed 10KB. This is normally not an issue and can only become an issue if you're using loops through a long list of records. Try to avoid using long loops.
For obvious reasons, server-side formulas cannot make use of the Document Object Model (DOM) or third-party JavaScript libraries. However, core JavaScript objects (Math, String and Date) are available for server-side scripting. If a formula or template is used within another formula or template, it will be evaluated before being used. However, this useful feature can result in endless recursion (consider Formula field my_formula with body {!my_formula}+1). To prevent endless recursion, the system limits maximum recursion level (number of times formula is called from within another formula) to 10.