JavaScript policy in apigee edge is taking a lot of time in execution of a recursion function

I am trying to recursively iterate over a long json payload in apigee edge JavaScript policy. For its execution apigee is taking 5x times more execution time than same javaScript function when I am trying on my local in Chrome browser . I would like to understand the possible cause of it, and remedies for it.

Picture 1st = time take by apigee edge javaScript policy 

Picture 2nd = time taken by local  JavaScript code 

DE2A6F39-D5EF-4446-ABE6-1AED90A81CB7.jpeg

7AC0D4C1-E69A-44D1-BD00-6509B2665556.jpeg



  

Solved Solved
0 3 637
1 ACCEPTED SOLUTION

The JS policy in Apigee runs in a Rhino JS interpreter. It's not JIT compiled and optimized like the v8 engine in Chrome. Logic might take longer running in an Apigee JS callout. There is no "remedy".  This is just how it works. 

If you have expensive computation, it shouldn't be running within the context of an Apigee API Proxy.  Move that logic into a separately managed application, maybe using Cloud Run, Cloud Functions, GKE, App Engine, or whatever. That way it can use v8, and a modern nodejs, and benefit from all the perf enhancements available. 

BTW, Your two images don't seem to show a 5x ratio.  One of them seems to indicate the computation takes ~48 seconds, the other ~52 seconds.  But I believe you that "things run more slowly in a JS callout running in Apigee."

 

View solution in original post

3 REPLIES 3

There's some existing discussions on this topic that are relevant and going into more detail

https://www.googlecloudcommunity.com/gc/Apigee/quot-If-performance-is-a-concern-for-custom-functiona...

As well as the docs here summarizes some of the major trade offs

https://docs.apigee.com/api-platform/fundamentals/best-practices-api-proxy-design-and-development#po...

In short, if performance is your priority, custom code in java will be the most performant as it will be able to run natively (unlike javascript).

The JS policy in Apigee runs in a Rhino JS interpreter. It's not JIT compiled and optimized like the v8 engine in Chrome. Logic might take longer running in an Apigee JS callout. There is no "remedy".  This is just how it works. 

If you have expensive computation, it shouldn't be running within the context of an Apigee API Proxy.  Move that logic into a separately managed application, maybe using Cloud Run, Cloud Functions, GKE, App Engine, or whatever. That way it can use v8, and a modern nodejs, and benefit from all the perf enhancements available. 

BTW, Your two images don't seem to show a 5x ratio.  One of them seems to indicate the computation takes ~48 seconds, the other ~52 seconds.  But I believe you that "things run more slowly in a JS callout running in Apigee."

 

Hi @dchiesa1 as per above images Time taken by recursive function in Apigee edge would be = 49 sec 252 milliseconds - 48 sec 686 milliseconds = 566 milliseconds. Similar way in google chrome it is 64 milliseconds . My bad for not giving more details on images . Thank you for reply and detailed explanation.