function makeImage( src ) {
var img = document.createElement( 'img' );
img.className='nail';
if ( src ) {
img.src='data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=';
}
return img;
}
withSrc = makeImage( true );
withoutSrc = makeImage( false );
makeImage( true );
makeImage( false );
withSrc.cloneNode();
withoutSrc.cloneNode();
const cloned = withoutSrc.cloneNode();
cloned.src = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
creation with src | |
creation without src | |
clone with src | |
clone without src | |
clone without src then set src |
Test name | Executions per second |
---|---|
creation with src | 49878.7 Ops/sec |
creation without src | 855427.1 Ops/sec |
clone with src | 49773.0 Ops/sec |
clone without src | 1308649.9 Ops/sec |
clone without src then set src | 53333.3 Ops/sec |
Overview of the Benchmark
MeasureThat.net is a website where users can create and run JavaScript microbenchmarks to compare the performance of different approaches. The provided benchmark measures the performance of creating and cloning img
elements in Firefox 80.
Script Preparation Code Analysis
The script preparation code defines a function makeImage(src)
that creates an img
element with the specified source URL. If src
is truthy, it sets the src
attribute of the img
element to a base64-encoded GIF image. The function returns the created img
element.
There are two variants of this function:
makeImage(true)
creates an img
element with a non-empty source URL.makeImage(false)
creates an empty img
element without a source URL.Options Comparison
The benchmark compares the performance of three approaches:
create with src
): This approach involves calling makeImage(true)
.creation without src
): This approach involves calling makeImage(false)
.clone without src then set src
): This approach involves cloning the result of makeImage(false)
using the cloneNode()
method, and then setting the src
attribute to a base64-encoded GIF image.Pros and Cons of Each Approach
src
attribute.img
element, which may not be desirable in some cases.Library and Special JS Features
The benchmark uses the cloneNode()
method, which is a built-in JavaScript method that creates a deep copy of an element.
No special JS features or syntax are used in this benchmark.
Other Alternatives
If you wanted to modify the benchmark to compare other approaches, here are some alternatives:
img
elements in different browsers or versions.These modifications would require additional script preparation code, test case definitions, and benchmark analysis.