function createWaitPromise( milliseconds ) {
return new Promise( ( resolve, reject ) => {
setTimeout( resolve, milliseconds );
});
}
if ( globalThis.SecondsPromise1 && globalThis.SecondsPromise10 && globalThis.SecondsPromise60 )
globalThis.SecondsPromise1 = globalThis.SecondsPromise10 = globalThis.SecondsPromise60 = null;
if ( !globalThis.SecondsPromise1 )
globalThis.SecondsPromise1 = createWaitPromise( 1000 * 1 );
/* Just busy waiting. */
if ( !globalThis.SecondsPromise10 )
globalThis.SecondsPromise10 = createWaitPromise( 1000 * 10 );
/* Just busy waiting. */
if ( !globalThis.SecondsPromise60 )
globalThis.SecondsPromise60 = createWaitPromise( 1000 * 60 );
/* Just busy waiting. */
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 second | |
10 seconds | |
60 seconds |
Test name | Executions per second |
---|---|
1 second | 1073529.2 Ops/sec |
10 seconds | 5624252.5 Ops/sec |
60 seconds | 5469197.5 Ops/sec |
Let's break down the provided benchmark and its components.
Benchmark Definition JSON
The benchmark is defined by a JSON object that contains information about the test. The relevant fields are:
Name
: A descriptive name for the benchmark.Description
: An empty string, indicating no description for this benchmark.Script Preparation Code
: A JavaScript function createWaitPromise
that returns a promise that resolves after a specified number of milliseconds.Html Preparation Code
: An empty string, indicating no HTML preparation code is needed.The script defines a global object globalThis
and checks if certain properties (SecondsPromise1
, SecondsPromise10
, and SecondsPromise60
) are defined. If any of them are not defined, they are assigned the return value of createWaitPromise
with different time intervals (1 second, 10 seconds, or 60 seconds). The comments indicate that these properties are used to create promises that can be waited upon.
Test Cases
The benchmark consists of three test cases:
Each test case has a Benchmark Definition
field that contains the actual JavaScript code to be executed. The code checks if any of the promises are defined and, if not, assigns them using createWaitPromise
. The comments indicate that these properties are used to create promises that can be waited upon.
Pros and Cons
The use of promises in this benchmark has both pros and cons:
Pros:
Cons:
Library: globalThis
The globalThis
object is not a standard library in JavaScript. It's an extension that provides access to global variables and functions on modern browsers. The globalThis
object is used here to access the properties SecondsPromise1
, SecondsPromise10
, and SecondsPromise60
.
Special JS Feature: Arrow Functions
The use of arrow functions (() => { ... }
) in the benchmark definition code is a feature introduced in ECMAScript 2015 (ES6). It provides a concise way to define small, one-time-use functions.
Other Alternatives
If this benchmark were to be rewritten, other alternatives could include:
setTimeout
and clearTimeout
instead of promises.async-promise-executor
or bluebird
for promise handling.It's worth noting that the choice of implementation depends on the specific requirements and constraints of the benchmark.