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();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Creation with src | |
Creation without src | |
Clone with src | |
Clone without src |
Test name | Executions per second |
---|---|
Creation with src | 135330.9 Ops/sec |
Creation without src | 2163599.0 Ops/sec |
Clone with src | 146972.9 Ops/sec |
Clone without src | 2435296.5 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Overview
The test case, "img src slowness", creates image tags with/without an src
attribute using JavaScript and measures their creation speed on various browsers. The test is primarily concerned with comparing the performance of creating an img
element with or without a src
attribute.
Options Compared
Two options are being compared:
img
element with a src
attribute: This option creates an img
element and sets its src
attribute to a data URI.img
element without a src
attribute: This option creates an img
element but does not set its src
attribute.Pros and Cons of Each Approach
img
element with a src
attribute:src
attribute.data URI
is large or complex, this approach can be slower due to the overhead of encoding and decoding the data.img
element without a src
attribute:data URI
.img
element.Library and Special JS Feature
The test case uses the cloneNode()
method, which is a DOM method that creates a copy of an existing node in the document. This feature is part of the JavaScript standard library.
Other Considerations
img
elements using cloneNode()
. This can be useful to compare the performance of creating multiple identical nodes versus copying an existing node.src
attribute is a common technique for serving small, binary images (e.g., icons or thumbnails) without requiring additional HTTP requests.Alternatives
If you wanted to create this benchmark with different approaches, here are some alternatives:
img
elements.appendChild()
or insertBefore()
, instead of cloneNode()
.URL.createObjectURL()
) versus using a data URI.By testing these different approaches, you can gain insights into the optimal way to create and manipulate HTML elements in JavaScript, which can help improve the performance and usability of your applications.