window.BaseLinkElement = document.createElement('a');
BaseLinkElement.target = "_blank";
window.LinkTemplate = document.createElement('template');
LinkTemplate.innerHTML = "<a target='_blank'></a>";
let a = document.createElement('a');
a.href = "my_custom_link";
a.textContent = "my_custom_text";
let a = window.BaseLinkElement.cloneNode(true);
a.href = "my_custom_link";
a.textContent = "my_custom_text";
let a = window.LinkTemplate.content.cloneNode(true);
a.href = "my_custom_link";
a.textContent = "my_custom_text";
let a = window.LinkTemplate.cloneNode(true).content;
a.href = "my_custom_link";
a.textContent = "my_custom_text";
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Create new element from scratch | |
Clone element and change | |
Clone template content and change | |
Clone template and use its content |
Test name | Executions per second |
---|---|
Create new element from scratch | 697686.6 Ops/sec |
Clone element and change | 648275.9 Ops/sec |
Clone template content and change | 687582.2 Ops/sec |
Clone template and use its content | 538037.5 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark definition measures the performance of creating multiple elements with different contents, specifically comparing cloning base elements versus not cloning them.
Benchmark Definition JSON Explanation
The benchmark definition JSON provides information about the test:
Name
and Description
: These fields provide context for the test, which is to compare the performance of creating new elements versus cloning existing ones.Script Preparation Code
: This section defines two variables: BaseLinkElement
, a base anchor element created with document.createElement('a')
, and LinkTemplate
, another anchor element created as a template with document.createElement('template')
. The script sets up these variables to be used in the test.Html Preparation Code
: This field is empty, indicating that no HTML preparation code is needed for this benchmark.Individual Test Cases
The individual test cases measure the performance of creating elements using different approaches:
let a = document.createElement('a');
) and sets its href
and textContent
attributes.window.BaseLinkElement.cloneNode(true)
) and modifies its href
and textContent
attributes.window.LinkTemplate.content.cloneNode(true)
) and sets its href
and textContent
attributes.window.LinkTemplate.cloneNode(true).content
) and uses it to create a new element with modified href
and textContent
attributes.Options Compared
The benchmark compares four options:
Pros and Cons of Each Approach
Here are some pros and cons for each approach:
Library Usage
The benchmark uses two libraries:
Template
(.LinkTemplate.content.cloneNode(true)
): Provides a way to create lightweight HTML templates.Special JS Features or Syntax
This benchmark does not use any special JavaScript features or syntax, apart from using a template element (<template>
), which is supported by most modern browsers.
Other Alternatives
If you're looking for alternative approaches to this benchmark, consider:
jsdom
or puppeteer
, which provide more control over HTML parsing and creation.Keep in mind that each alternative approach may have trade-offs in terms of performance, complexity, or ease of use.