function wait(ms) {
return new Promise(res => setTimeout(() => { res(ms); }, ms));
}
function factorializeRecursive(num) {
if (num < 0)
return -1;
else if (num == 0)
return 1;
else {
return (num * factorializeRecursive(num - 1));
}
}
setTimeout(function () { deferred.resolve() }, 50);
setTimeout(function () { deferred.resolve() }, 500);
setTimeout(function () { deferred.resolve() }, 100);
await wait(100);
deferred.resolve();
var r = factorializeRecursive(5000);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Deferred wait 50 | |
Deferred wait 500 | |
Deferred wait 100 | |
Async Wait 100 | |
Regular/Sync Test, 5000! |
Test name | Executions per second |
---|---|
Deferred wait 50 | 18.2 Ops/sec |
Deferred wait 500 | 2.0 Ops/sec |
Deferred wait 100 | 9.6 Ops/sec |
Async Wait 100 | 9.6 Ops/sec |
Regular/Sync Test, 5000! | 7410.5 Ops/sec |
The benchmark titled "Deferred Test" focuses on evaluating the performance of deferred execution in JavaScript, specifically using the setTimeout
function to defer the resolution of a deferred
object. In this case, the benchmark assesses how different delay times in milliseconds (50ms, 100ms, and 500ms) impact the number of executions per second.
Deferred Wait 50ms
setTimeout(function () { deferred.resolve() }, 50);
Deferred Wait 100ms
setTimeout(function () { deferred.resolve() }, 100);
Deferred Wait 500ms
setTimeout(function () { deferred.resolve() }, 500);
Shorter Wait Times (50ms)
Medium Wait Time (100ms)
Longer Wait Times (500ms)
Deferred Objects:
While not specifically mentioned in the Benchmark Definition
, the term "deferred" typically refers to a programming pattern often related to promises in JavaScript, allowing asynchronous execution and resolution. Deferred objects are commonly used in libraries like jQuery or implementations that support Promises, facilitating better control over asynchronous operations.
Promises: Instead of using setTimeout
with deferred resolutions, JavaScript developers frequently leverage Promises for managing asynchronous operations. Promises help to create a cleaner and more manageable approach to handle asynchronous flows, providing built-in methods like .then()
and error handling with .catch()
.
Async/Await: With the introduction of async/await
, JavaScript simplifies working with asynchronous operations. It allows developers to write asynchronous code that looks more like synchronous code, making it easier to read and maintain.
Web Workers: For heavy computations or tasks that can run independently of the main thread, developers may consider utilizing Web Workers. This technique can provide an alternative to deferring tasks through setTimeout
, ensuring responsive UI while executing heavier jobs.
In conclusion, the benchmark primarily tests the impact of various wait times on deferred executions using setTimeout
. Each approach has its inherent pros and cons depending on the performance impact the user desires. Alternative methods such as Promises and async/await may offer more modern and maintainable solutions for handling asynchronous tasks.