Home >Web Front-end >JS Tutorial >Is `return await` a Performance Bottleneck in JavaScript?

Is `return await` a Performance Bottleneck in JavaScript?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-20 02:24:01922browse

Is `return await` a Performance Bottleneck in JavaScript?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn