<form class="test-form">
<input class="test-element" />
</form>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.js'></script>
var $context = $('.test-form');
var context = document.querySelector('.test-form');
$('.test-element', $context);
$('.test-form').find('.test-element');
$context.find('.test-element');
context.querySelector('.test-element');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
$(selector, $context) | |
$(selector).find(selector) | |
$context.find(selector) | |
Vanilla querySelector |
Test name | Executions per second |
---|---|
$(selector, $context) | 1664995.5 Ops/sec |
$(selector).find(selector) | 1147342.2 Ops/sec |
$context.find(selector) | 1844792.5 Ops/sec |
Vanilla querySelector | 6818134.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The benchmark compares three approaches for selecting elements in a web page:
querySelector
, but takes an additional context
parameter to narrow down the search.Options Comparison
The three options are compared in terms of:
ExecutionsPerSecond
).Pros and Cons
Here's a brief summary of the pros and cons for each approach:
Library and Syntax
$(selector, context)
syntax is convenient but may be slower than native methods.context
.Special JS Features and Syntax
None mentioned in this benchmark.
Alternatives
If you're looking for alternatives to MeasureThat.net or want to explore other benchmarking tools:
Keep in mind that the results of these benchmarks can vary depending on the specific hardware, software, and browser versions used.