Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sync vs Promise benchmark test
(version: 0)
Comparing performance of:
Sync vs Promise
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function foo() { const array = []; const count = 1000000; for (let i = 0; i < count; i++) { array.push(Math.random() * count); } return array.filter(num => num > count / 2); }
Tests:
Sync
const run = () => { const result = foo(); console.log(result); }; run();
Promise
const run = async () => { const promise = new Promise(resolve => { resolve(foo()); }); const result = await promise; console.log(result); } run();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Sync
Promise
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Mobile Safari/537.36 OPR/89.0.0.0
Browser/OS:
Opera Mobile 89 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Sync
15.5 Ops/sec
Promise
13.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and discussed. **Benchmark Definition:** The benchmark definition provides two scripts to test: one that uses synchronous code (`foo` function) and another that uses an asynchronous code with promises (`run async function`). **Script Preparation Code (Sync):** ```javascript function foo() { const array = []; const count = 1000000; for (let i = 0; i < count; i++) { array.push(Math.random() * count); } return array.filter(num => num > count / 2); } ``` This script creates an array of 1 million random numbers, filters the numbers greater than half of the total count, and returns the result. The `foo` function is a synchronous function that runs sequentially. **Script Preparation Code (Promise):** ```javascript const run = async () => { const promise = new Promise(resolve => { resolve(foo()); }); const result = await promise; console.log(result); } ``` This script creates a promise with the `foo` function as its resolution value. The `await` keyword is used to wait for the promise to be resolved and then logs the result. **Comparison:** The benchmark compares two approaches: 1. **Synchronous Code (Sync):** * Pros: * Simpler code structure * Less memory-intensive compared to promises * Cons: * Blocking, which means that it will wait for each operation to complete before moving on to the next one. * Can cause UI freezes or other performance issues if used in browser-side applications. 2. **Asynchronous Code with Promises (Promise):** * Pros: * Non-blocking, allowing for more efficient use of system resources * Can handle multiple operations concurrently using `async/await` and promises * Cons: * More complex code structure compared to synchronous code. * Requires additional memory due to the creation of new promise objects. **Library:** None are explicitly mentioned in this benchmark, but the use of promises and async/await keywords implies that modern JavaScript environments (e.g., Node.js or browser-side) support these features. The `console` object is also used, which suggests a standard global environment for logging results. **Special JS Features/Syntax:** The use of promises (`const promise = new Promise(resolve => { resolve(foo()); });`) and async/await (`const result = await promise;`) syntax is highlighted in the benchmark. This is modern JavaScript syntax that allows for asynchronous programming without the need for callbacks or polling. **Other Alternatives:** * **Callbacks:** Instead of promises, you can use callback functions (e.g., `foo()`). However, using callbacks leads to "callback hell" issues where the code becomes harder to read and maintain. * **Web Workers:** If you're running computationally intensive tasks on a browser-side application, you might consider using web workers. Web workers are worker threads in web browsers that can run JavaScript code concurrently with the main thread. However, they introduce additional complexity. The benchmark allows users to compare performance between synchronous and asynchronous approaches for their own use cases, providing insight into the trade-offs between simplicity, memory usage, and system resource efficiency when writing code in modern JavaScript environments.
Related benchmarks:
sync vs promise
async vs sync 337-(839
Sync vs Promise benchmark test (ver 1)
Sync vs Promise benchmark test (ver 2)
Comments
Confirm delete:
Do you really want to delete benchmark?