Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Check if is thennable, check if is instanceof Promise, or just await
(version: 0)
Comparing performance of:
Check await vs Instanceof await vs Just await
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
async function checkAwait(value) { const result = (typeof value.then === 'function') ? await value : value; console.log(result); } async function checkAwaitTest() { checkAwait(1); checkAwait(Promise.resolve(1)); checkAwait(1); checkAwait(Promise.resolve(1)); checkAwait(1); checkAwait(Promise.resolve(1)); checkAwait(1); checkAwait(Promise.resolve(1)); checkAwait(1); checkAwait(Promise.resolve(1)); } async function instanceofAwait(value) { const result = value instanceof Promise ? await value : value; console.log(result); } async function instanceofAwaitTest() { instanceofAwait(1); instanceofAwait(Promise.resolve(1)); instanceofAwait(1); instanceofAwait(Promise.resolve(1)); instanceofAwait(1); instanceofAwait(Promise.resolve(1)); instanceofAwait(1); instanceofAwait(Promise.resolve(1)); instanceofAwait(1); instanceofAwait(Promise.resolve(1)); } async function justAwait(value) { const result = await value; console.log(result); } async function justAwaitTest() { justAwait(1); justAwait(Promise.resolve(1)); justAwait(1); justAwait(Promise.resolve(1)); justAwait(1); justAwait(Promise.resolve(1)); justAwait(1); justAwait(Promise.resolve(1)); justAwait(1); justAwait(Promise.resolve(1)); }
Tests:
Check await
checkAwaitTest();
Instanceof await
instanceofAwaitTest();
Just await
justAwaitTest();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Check await
Instanceof await
Just await
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the provided benchmark definition and its test cases. **Overview** The benchmark measures the performance of three different approaches for checking if a value is thenable, checking if it's an instance of a Promise, or simply awaiting it. The tests are designed to compare the execution time of these approaches in a JavaScript context. **Test Cases** There are three individual test cases: 1. **Check await**: This test case calls `checkAwait` function with different inputs (number and a resolved Promise) repeatedly. 2. **Instanceof await**: This test case calls `instanceofAwait` function with different inputs (number and a resolved Promise) repeatedly. 3. **Just await**: This test case calls `justAwait` function with different inputs (number and a resolved Promise) repeatedly. **Script Preparation Code** The script preparation code is an asynchronous JavaScript file that defines three functions: * `checkAwait`: This function takes a value as input, checks if it's thenable using the `await` keyword, and logs the result to the console. * `checkAwaitTest`: This function calls `checkAwait` with different inputs repeatedly to test its performance. * `instanceofAwait`: This function takes a value as input, checks if it's an instance of a Promise using the `instanceof` operator, and logs the result to the console. * `instanceofAwaitTest`: This function calls `instanceofAwait` with different inputs repeatedly to test its performance. * `justAwait`: This function takes a value as input and awaits it directly, logging the result to the console. * `justAwaitTest`: This function calls `justAwait` with different inputs repeatedly to test its performance. **Library** None of these functions use any external libraries. The `Promise` constructor is used to create a resolved Promise, which is passed as an argument to the `checkAwait` and `instanceofAwait` functions. **Special JS Feature or Syntax** The `await` keyword in JavaScript ES6+ syntax is used in the `checkAwait` function. This allows the function to asynchronously wait for the value to complete before returning its result. **Pros and Cons of Different Approaches** 1. **Check await**: * Pros: Uses the `await` keyword, which is a built-in feature in modern JavaScript. * Cons: May be slower due to the overhead of asynchronous execution. 2. **Instanceof await**: * Pros: Checks if the value is an instance of a Promise using the `instanceof` operator, which can be faster than using `await`. * Cons: Requires explicit type checking and may not work with non-Promise values. 3. **Just await**: * Pros: Directly awaits the value without any checks or optimizations. * Cons: May be slower due to the overhead of asynchronous execution and requires more memory. **Other Alternatives** Other approaches could include: 1. Using `Promise.resolve()` to create a Promise from a non-Promise value. 2. Using the `thenable` property on objects to check if they are thenable. 3. Implementing a custom function that checks for both `instanceof` and `thenable` properties. However, these alternatives may not be as efficient or readable as the provided approaches, and their performance may vary depending on the specific use case.
Related benchmarks:
then vs. async-await
Promise vs Async Await
Check await or just await
Promise vs Async Await(2)
Comments
Confirm delete:
Do you really want to delete benchmark?