Salesforce Functions is deprecated and will soon not be available at all (APRIL 2024). I will leave this project up as an interesting experiment, and perhaps the basis of similar performance tests in future.
Salesforce Functions employ elastic computing for compute heavy tasks, and are good a candidate to reduce high cpu tasks currently running in Apex.
I thought it would be interesting to see how far I could push them compared to Apex and browser Javascript.
This recipe is a demonstration of an intensive mathmatical algorithm that I have implemented as:
1. A static APEX method 2. A Salesforce Function (Javascript type) 3. A Javascript function in an LWC control |
When run in a Developer Sandbox:
Yields these results:
These statistics have informed the following observations:
- Salesforce Functions are demonstrably more scalable than Apex with heavy tasks
- Salesforce Functions can be slower than Apex with low intensity CPU tasks, so do test and don't assume that Functions are faster for your scenario
- The web browser is the fastest place that you can run Javascript, and at no extra cost. It is worth considering if you have demanding CPU tasks and don't need data security or complete reliability
This project should be easy to deploy as long as you have a Functions license. Unfortunately this won't work with a local Docker environment in lieu of Functions in your Org.
- Open the Project in Visual Studio Code
- Setup your Salesforce Sandbox as per Salesforce guidelines
- With SFDX, deploy the metadata in the Project to your sandbox
- With SFDX, deploy the Function in the Project to the corresponding Compute environment
- Find a page in your Org to add the 'cpuTest' custom control to it
- This Function example does not use the Salesforce API at all
- This test was performed in Winter '22, and apparently the Compute instance is located in North America at this time. When Compute instances are released next to Orgs in my goegraphy then latency times can be assumed to improve
- 'Start up cost' - you may have to run the functiona few times before it finds its optimal run speed
- It is rather unfair to compare to recent web browser javascript implementations, which are blazing fast. Containers are not designed for that.
- Origional source code for cpu intensive calculation: https://gist.github.com/sqren/5083d73f184acae0c5b7
- Apex is actually faster than Salesforce Functions for less intensive CPU bound workloads
- Apex has a limit (base number 8 in this calculation) that it can actually handle
- Salesforce Functions in Javascript are much slower than the same Javascript running on the browser
- Browser side Javascript is actually insanely fast, esspecially on Apple iOS devices
- Local Docker performance was not tested. I didn't see the need given that this can't be used in Production.