There is no guarantee that the state of a Cloud function will be preserved for future invocations. However, Cloud Functions often recycles the execution environment of a previous invocation. If you declare a variable with a global scope, its value can be reused in subsequent invocations without having to be recomputed.
This way, you can cache objects that may be expensive to recreate on each function invocation. Moving such objects from the function body to a global scope may result in significant performance improvements.