<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<div id="outer">
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div id="inner">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="outer2">
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div id="inner2">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
var inner = document.getElementById("inner");
var outer = document.getElementById("outer");
var inner2 = document.getElementById("inner2");
var outer2 = document.getElementById("outer2");
var x = jQuery.contains(outer, inner)
var y = jQuery.contains(outer, inner2)
var z = jQuery.contains(outer2, inner)
var a = jQuery.contains(outer2, inner2)
var x = outer.contains(inner)
var y = outer.contains(inner2)
var z = outer2.contains(inner)
var a = outer2.contains(inner2)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery.contains() | |
Node.contains() |
Test name | Executions per second |
---|---|
jQuery.contains() | 1382597.5 Ops/sec |
Node.contains() | 1970337.6 Ops/sec |
Let's dive into the world of JavaScript benchmarks!
What is being tested?
The provided benchmark tests two different ways to check if an element contains another element: jQuery's contains()
method and the built-in Node.contains()
method.
In the first test case, jQuery.contains()
, the script uses jQuery's contains()
function to check if the inner
element is contained within the outer
element. The same test is repeated with the inner2
element. In contrast, the second test case, Node.contains()
, uses the built-in Node.contains()
method to achieve the same result.
Options being compared
The benchmark compares two options:
Pros and Cons
Here are some pros and cons of each approach:
Library usage
In this benchmark, jQuery is used as a library to provide the contains()
function. This means that users who want to use this test case must include the jQuery library in their project.
Special JS feature or syntax
There are no special JavaScript features or syntax being tested here. The benchmark only involves using the built-in Node.contains()
method and jQuery's contains()
function.
Other alternatives
If you're interested in exploring alternative methods, here are a few options:
Node.contains()
but is specific to the DOM API.Keep in mind that these alternatives may not provide exactly the same results as the built-in Node.contains()
or jQuery's contains()
methods, so they should be used with caution.