<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div>
<div class="div1"><span>stuff</span><span>stuff</span><span>stuff</span><span>stuff</span><span>stuff</span><span>stuff</span><span>stuff</span><span>stuff</span><span>stuff</span><h1>H1</h1><span>stuff</span><span>stuff</span><span>stuff</span><span>stuff</span><span>stuff</span><span>stuff</span><h2>h2</h1></div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div><div>stuff</div>
$(".div1 h2");
$(".div1 h3");
var $div = $('.div1');
$div.find('h2');
$div.find('h3');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
select | |
find |
Test name | Executions per second |
---|---|
select | 38131.2 Ops/sec |
find | 177466.9 Ops/sec |
I'll break down the test cases and explain what's being tested, comparing different approaches, their pros and cons, and other considerations.
Benchmark Context
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark in question compares the performance of two methods: select
and find
.
Script Preparation Code
The script preparation code includes an external jQuery library reference, which is used by both test cases. This library provides functions for selecting elements on the page.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
Individual Test Cases
There are two test cases:
select
Test CaseThe first test case uses the $("selector")
syntax to select an element on the page.
$(".div1 h2");
$(".div1 h3");
This approach is specific to jQuery, a popular JavaScript library for DOM manipulation.
Pros:
Cons:
find
Test CaseThe second test case uses a vanilla JavaScript approach to find elements on the page.
var $div = $('.div1');
$div.find('h2');
$div.find('h3');
This approach relies on the document.querySelector()
method, which is part of the modern web platform ( ECMAScript 2015+).
Pros:
Cons:
select
approach, as it requires parsing the CSS selector string and traversing the DOM tree.document.querySelector()
.Library: jQuery
jQuery is a popular JavaScript library for DOM manipulation. It provides functions like $()
, .find()
, and .on()
for selecting and manipulating elements on the page. The library also offers various utility methods, such as .trim()
, .html()
, and .text()
, for working with HTML content.
Special JS Feature: No special features are used in this benchmark
No additional JavaScript features or syntax are being tested beyond vanilla JavaScript and jQuery.