<div>
<div>dddd</div><span>dddd</span>
<script>
//--->
</script>
<div>
</div>
</div>
var body = document.body;
body.innerHTML.indexOf("script")
body.getElementsByTagName("scirpt")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
innerhtml | |
find |
Test name | Executions per second |
---|---|
innerhtml | 553931840.0 Ops/sec |
find | 475500960.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is defined by two test cases:
innerhtml
: Tests the execution time of body.innerHTML.indexOf("script")
.find
: Tests the execution time of body.getElementsByTagName("scirpt")
.Options Compared
The two options compared are:
innerHTML
with indexOf()
getElementsByTagName()
with a substring (in this case, "scirpt")Pros and Cons
innerHTML
with indexOf()
getElementsByTagName()
with a substringLibrary and Purpose
None of the test cases explicitly use any libraries, but it's worth noting that getElementsByTagName()
returns a live HTMLCollection, which is a collection of nodes in the DOM. The purpose of this function is to retrieve all elements with a specific tag name.
Special JS Feature or Syntax
There are no special features or syntaxes being tested here.
Alternative Approaches
Other alternatives for testing string searching and retrieval could include:
String.prototype.indexOf()
(which is faster than the DOM's innerHTML
property)However, these alternatives might not be directly relevant to this specific benchmark, as it focuses on comparing two common DOM-related methods.
Benchmark Preparation Code
The preparation code for each test case is:
var body = document.body;
This line retrieves the body
element of the HTML document, which is a common practice when working with the DOM in JavaScript.