function delayedValue( milliseconds, value ) {
return new Promise( ( resolve /*, reject*/ ) => {
setTimeout( () => resolve( value ), milliseconds );
} );
}
function createBlobObjectURL( milliseconds, value ) {
let codes = ``
+ `function delayedValue( milliseconds, value ) {\n`
+ ` return new Promise( ( resolve /*, reject*/ ) => {\n`
+ ` setTimeout( () => resolve( value ), milliseconds );\n`
+ ` } );\n`
+ `}\n`
+ `await delayedValue( ${milliseconds}, ${value} );`
;
let blob = new Blob( [ codes ], { type: "text/javascript" } );
let theBlobObjectURL = URL.createObjectURL( blob );
return theBlobObjectURL;
}
let theBlobObjectURL = createBlobObjectURL( 1 * 1000, 1 );
import( theBlobObjectURL );
URL.revokeObjectURL( theBlobObjectURL );
let theBlobObjectURL = createBlobObjectURL( 5 * 1000, 2 );
import( theBlobObjectURL );
URL.revokeObjectURL( theBlobObjectURL );
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 seconds | |
5 seconds |
Test name | Executions per second |
---|---|
1 seconds | 256.6 Ops/sec |
5 seconds | 250.0 Ops/sec |
Let's break down the provided JSON and explain what is tested, the options compared, pros and cons of each approach, and other considerations.
Benchmark Definition
The benchmark definition represents a JavaScript function that creates a Blob object URL using the createBlobObjectURL
function. This function generates a JavaScript code snippet that defines the delayedValue
function, which returns a promise that resolves after a specified delay.
"Script Preparation Code": "function delayedValue( milliseconds, value ) {\r\n return new Promise( ( resolve /*, reject*/ ) => {\r\n setTimeout( () => resolve( value ), milliseconds );\r\n } );\r\n}\r\n\r\nfunction createBlobObjectURL( milliseconds, value ) {\r\n let codes = ``\r\n + `function delayedValue( milliseconds, value ) {\\n`\r\n + ` return new Promise( ( resolve /*, reject*/ ) => {\\n`\r\n + ` setTimeout( () => resolve( value ), milliseconds );\\n`\r\n + ` } );\\n`\r\n + `}\\n`\r\n + `await delayedValue( ${milliseconds}, ${value} );`\r\n ;\r\n\r\n let blob = new Blob( [ codes ], { type: \"text/javascript\" } );\r\n let theBlobObjectURL = URL.createObjectURL( blob );\r\n return theBlobObjectURL;\r\n}\r\n"
Options Compared
The benchmark definition compares two approaches:
createBlobObjectURL
function generates a JavaScript code snippet that is embedded directly into the codes
variable.blob
object.Pros and Cons
createBlobObjectURL
function.Other Considerations
createBlobObjectURL
function uses the URL.createObjectURL()
method to create a blob object URL. This allows the generated JavaScript code to be executed in a sandboxed environment.Library: URL
The URL
library is used to create a blob object URL. The URL.createObjectURL()
method takes an array of byte-like values (in this case, a string containing JavaScript code) as input and returns a URL that can be used to access the blob.
Special JS Feature/Syntax
createBlobObjectURL
function uses template literals to generate the JavaScript code snippet. This feature is available in modern JavaScript engines.await
keyword to simulate asynchronous behavior. This feature is also available in modern JavaScript engines.Alternatives
Other alternatives for creating blob object URLs include:
jszip
fetch()
API with a blob as the response