<html>
<head>
</head>
<body>
<div id="div">
<div id="a">
</div>
</div>
<div id="b">
</div>
</body>
</html>
const { div, a, b } = window
for (let i = 0; i < 100; i++) {
let compare = div.compareDocumentPosition(a)
compare & 8
compare & 16
compare = div.compareDocumentPosition(b)
compare & 8
compare & 16
}
const { div, a, b } = window
for (let i = 0; i < 100; i++) {
div.contains(a)
a.contains(div)
div.contains(b)
b.contains(div)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
compareDocumentPosition | |
contains |
Test name | Executions per second |
---|---|
compareDocumentPosition | 89980.8 Ops/sec |
contains | 212592.3 Ops/sec |
Let's break down what is being tested in this benchmark.
Benchmark Definition
The benchmark compares the performance of two methods: compareDocumentPosition
and contains
, both used to determine if an element (in this case, div
) contains another element (a
or b
). These methods are part of the Document Object Model (DOM) and are used extensively in web development.
Options Compared
The benchmark compares two approaches:
contains
method. The values can be:Pros and Cons
compareDocumentPosition:
Pros:
Cons:
contains:
Pros:
compareDocumentPosition
.Cons:
compareDocumentPosition
in some cases.Other Considerations
When using these methods, it's essential to consider the following:
Library and Purpose
In this benchmark, window.div
is assumed to be a reference to a DOM element. The div
object has a compareDocumentPosition
method that takes another element as an argument. This method returns the result of the comparison. Additionally, div.contains()
is used as a convenience method to check containment.
Special JS Feature or Syntax
There are no special JavaScript features or syntaxes being tested in this benchmark. Both methods (compareDocumentPosition
and contains
) are standard DOM methods that have been part of JavaScript for a long time.
Alternatives
Other alternatives to these methods include:
:contains
pseudo-class.