Performance Implications of return await
The ESLint rule "no-return-await" advises against the use of return await expressions. However, it claims this practice adds extra time before a Promise resolves or rejects.
Is return await a Performance Problem?
According to the MDN async function documentation, a "Simple Example" illustrates the use of return await without any caution regarding performance concerns.
Actual Performance Impact
Notably, return await does not introduce any significant performance issues. It merely adds an unnecessary operation that might slightly increase execution time, comparable to returning x 0 instead of x for an integer x.
Bad Style and Lack of Understanding
While not harmful, return await is considered poor style and may indicate a lack of thorough comprehension of promises and async/await.
A Notable Exception
However, in cases like:
try { … return await …; } …
return await plays a critical role. Unlike a plain return, await throws on Promise rejections and ensures that the specified Promise resolves before any following catch or finally handlers are executed. This ensures that error handling is properly handled.
The above is the detailed content of Is `return await` a Performance Bottleneck in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!