<form id="userForm">
<input type="text" name="name"/>
<input type="password" name="password"/>
<input type="checkbox" name="isHaveExperience" value="true"/>
<input type="radio" name="difficult" value="easy"/>
<input type="radio" name="difficult" value="medium"/>
<input type="radio" name="difficult" value="hard"/>
</form>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js'></script>
var $userFormElement = $("#userForm");
let $searchElements = $userFormElement.find("[type!=password][type!=checkbox][type!=radio]");
let $searchElements = $userFormElement.find(":not([type=password]):not([type=checkbox]):not([type=radio])");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
using not equal attribute | |
using :not selector |
Test name | Executions per second |
---|---|
using not equal attribute | 55109.7 Ops/sec |
using :not selector | 109580.9 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Overview
The benchmark is designed to measure the performance of jQuery's selector engine on finding elements that do not match three specific input types (password, checkbox, or radio). The goal is to determine which approach is faster: using a "not equal" attribute in the selector or using the :not
pseudo-class.
Script Preparation Code
The script preparation code creates a $userFormElement
variable by selecting an HTML form element with the ID userForm
. This variable will be used as a context for the selection.
Html Preparation Code
The HTML preparation code defines an HTML form with several input elements, including:
This setup allows us to test the selector engine on finding elements that do not match any of the three specified input types.
Individual Test Cases
There are two test cases:
** The first test case uses the "not equal" attribute in the selector:
$userFormElement.find("[type!=password][type!=checkbox][type!=radio]");`. This approach directly specifies the allowed input types using an attribute.** The second test case uses the
:notpseudo-class in the selector:
$userFormElement.find(":not([type=password]):not([type=checkbox]):not([type=radio])");`. This approach uses a different syntax to specify the excluded input types.Library and Purpose
In this benchmark, jQuery is used as a library. Its purpose is to provide a robust and efficient way to select elements on an HTML document. In this case, it's used to find elements that match specific conditions (in or not in certain input types).
Special JS Feature or Syntax
This benchmark uses the :not
pseudo-class, which is a feature introduced in jQuery 1.9. The syntax is different from other CSS selectors and requires a good understanding of how it works.
Pros and Cons
Here are some pros and cons for each approach:
:not
works with CSS selectorsOther Alternatives
There are other alternatives to approach this benchmark:
Overall, both approaches have their pros and cons, and the best choice depends on the specific requirements and constraints of your project.