Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Callback vs Promise2
(version: 2)
Comparing performance of:
Promise vs Callback
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function carlosWithPromise() { return new Promise((resolve, reject) => { setTimeout(() => { return resolve(5); }); }) } function carlosWithCallback(callback) { window.setTimeout(() => { callback(100); }); }
Tests:
Promise
carlosWithPromise() .then(parametro => { console.log('parametro: ', parametro); });
Callback
carlosWithCallback((param) => console.log('params::', param));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Promise
Callback
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Promise
1260544.2 Ops/sec
Callback
1386598.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark measures the performance difference between two approaches: 1. **Callback**: Using a callback function to handle the asynchronous result of `setTimeout()`. 2. **Promise2**: Using a promise (specifically, a `.then()` method) to handle the asynchronous result of `setTimeout()`. **Script Preparation Code** Both scripts contain similar code: ```javascript function carlosWithPromise() { return new Promise((resolve, reject) => { setTimeout(() => { resolve(5); }); }) } function carlosWithCallback(callback) { window.setTimeout(() => { callback(100); }); } ``` The main difference lies in how the asynchronous result is handled: **Callback**: The `carlosWithCallback` function takes a callback function as an argument, which is called when the `setTimeout()` timer expires. In this case, the callback simply logs "params::" followed by the value passed to it (which is always 100). **Promise2**: The `carlosWithPromise` function returns a promise that resolves to the value 5 after a timeout of 0 milliseconds using `setTimeout()`. The `.then()` method is used to specify what should happen when the promise is resolved. **Pros and Cons** 1. **Callback**: * Pros: Simple, lightweight, and widely supported. * Cons: Error handling can be cumbersome, and it's easy to forget to cancel promises (e.g., due to memory leaks). 2. **Promise2**: * Pros: Provides a more modern, object-oriented way of handling asynchronous code, with built-in support for error handling and cancellation. * Cons: Can be overkill for simple use cases, and some older browsers might not support it. **Library/External dependency** None are explicitly mentioned in the provided JSON. However, `Promise2` uses the native JavaScript Promise API, which is a built-in feature of modern browsers and Node.js. **Special JS features/syntax** There's no mention of any special JavaScript features or syntax that would require additional explanation. **Other alternatives** For measuring performance differences between callbacks and promises, you can also consider using other alternatives like: 1. **Closures**: Using a closure to handle the asynchronous result in both approaches. 2. **Async/await**: A more modern way of handling asynchronous code, which is syntactically similar to promises but provides better error handling and readability. Keep in mind that the choice of approach depends on your specific use case and personal preference.
Related benchmarks:
Promise vs. Callback
just promise vs just callback
Promise vs async vs callbacks
callbacks vs promises vs async/awaits
Comments
Confirm delete:
Do you really want to delete benchmark?