You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some async functions, you're using redundant code that makes the execution code slower (I assume that this is also valid for the deno sdk). I didn't new at first that this redundant code could make the execution slower, but I tested it and here is my experience/results.
In some async functions, you're using the following pattern (from /lib/services/account.js):
From this pov, I decided to benchmark the 2 solutions with the following script:
asyncfunctionmeasure_async_func(cb){conststart=performance.now()awaitcb()conststop=performance.now()consttimeInSeconds=(stop-start)/1000;consttimeRounded=timeInSeconds.toFixed(3)returntimeRounded}functionprintExecutionTime(funcName,executionTime){console.log(`Time taken to execute function ${funcName}(): ${executionTime} seconds`)}functiongetNumber(){returnnewPromise((resolve,_)=>{resolve(10)})}functiongetNumber1(){returngetNumber()}asyncfunctiongetNumber2(){returnawaitgetNumber()}asyncfunctiongetNumbers1(numbersToGenerate){for(leti=0;i<numbersToGenerate;i++){awaitgetNumber1()}}asyncfunctiongetNumbers2(numbersToGenerate){for(leti=0;i<numbersToGenerate;i++){awaitgetNumber2()}}asyncfunctionrunTests(){constnumbersToGenerate=10_000_000constexecutionTime1=awaitmeasure_async_func(()=>getNumbers1(numbersToGenerate))printExecutionTime("getNumbers1",executionTime1)constexecutionTime2=awaitmeasure_async_func(()=>getNumbers2(numbersToGenerate))printExecutionTime("getNumbers2",executionTime2)constratio=executionTime2/executionTime1console.log(`getNumbers1() is ${ratio} faster than getNumbers2()`)}runTests()
The results are (with Node v17.4.0):
Time taken to execute function getNumbers1(): 0.928 seconds
Time taken to execute function getNumbers2(): 1.613 seconds
getNumbers1() is 1.738146551724138 faster than getNumbers2()
I haven't tested with the sdk itself, but with a simple script, the second solution (without "async" and "await" notation) seems 1.74 times faster than the first one. I should have tested with the sdk itself, but I don't really have time for that rn.
The text was updated successfully, but these errors were encountered:
JulienLecoq
changed the title
Improve performance with async functions
Improve performance for async functions
Feb 18, 2022
In some async functions, you're using redundant code that makes the execution code slower (I assume that this is also valid for the deno sdk). I didn't new at first that this redundant code could make the execution slower, but I tested it and here is my experience/results.
In some async functions, you're using the following pattern (from /lib/services/account.js):
This pattern contains an unnecessary "async" and "await" notation.
This could be simplified by:
From this pov, I decided to benchmark the 2 solutions with the following script:
The results are (with Node v17.4.0):
I haven't tested with the sdk itself, but with a simple script, the second solution (without "async" and "await" notation) seems 1.74 times faster than the first one. I should have tested with the sdk itself, but I don't really have time for that rn.
The text was updated successfully, but these errors were encountered: