Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
querySelector shorthand
(version: 0)
querySelector shorthand vs vanilla JS
Comparing performance of:
Vanilla get text vs Shorthand get text vs Vanilla set text vs Shorthand set text
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script> const DOC = (s,c) => (c ?? document).querySelector(s); </script> <div id="foo">Hello World</div>
Tests:
Vanilla get text
let text = document.querySelector("#foo").innerText;
Shorthand get text
let text = DOC("#foo").innerText;
Vanilla set text
document.querySelector("#foo").innerText = "Hello There";
Shorthand set text
DOC("#foo").innerText = "Hello There";
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Vanilla get text
Shorthand get text
Vanilla set text
Shorthand set text
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Definition JSON** The benchmark defines two variables: `DOC` and an HTML document with a single element `<div id="foo">Hello World</div>`. * `DOC` is a function that takes two arguments: `s` (selector) and `c` (document context). If `c` is not provided, it defaults to the current document object. The purpose of `DOC` is to provide a convenient way to query elements using the shorthand syntax. * The HTML document contains a `<div>` element with an ID of "foo" and the text content "Hello World". **Individual Test Cases** There are four test cases: 1. **Vanilla get text**: This test case measures the execution time of retrieving the inner text of the `#foo` element using vanilla JavaScript's `document.querySelector()` method. 2. **Shorthand get text**: This test case measures the execution time of retrieving the inner text of the `#foo` element using the shorthand syntax defined by `DOC`. 3. **Vanilla set text**: This test case measures the execution time of setting the inner text of the `#foo` element using vanilla JavaScript's `document.querySelector()` method. 4. **Shorthand set text**: This test case measures the execution time of setting the inner text of the `#foo` element using the shorthand syntax defined by `DOC`. **Comparison Options** The benchmark compares two approaches: * **Vanilla JS**: Using vanilla JavaScript methods, such as `document.querySelector()` and `innerText`. * **Shorthand (via DOC)**: Using the shorthand syntax defined by `DOC`, which is a convenience wrapper around vanilla JavaScript's querySelector() method. **Pros and Cons of Each Approach** **Vanilla JS** Pros: * Well-established and widely supported * Can be used in any context, without relying on a specific library or implementation Cons: * May be slower than the shorthand syntax, due to the overhead of calling `querySelector()` and accessing `innerText` * Requires more boilerplate code to achieve similar results **Shorthand (via DOC)** Pros: * Provides a convenient and expressive way to query elements * Can be faster than vanilla JS, since it avoids the overhead of calling `querySelector()` Cons: * Relies on a specific implementation (`DOC`) that may not be compatible with all browsers or environments * May require more code changes if the implementation is modified or updated **Other Considerations** * The benchmark uses Chrome 98 and Windows as the test platform, which may not be representative of other platforms or browsers. * The test cases focus on retrieving and setting inner text content, but do not account for other use cases, such as querying child elements or manipulating attribute values. **Alternatives** If you're interested in exploring alternative benchmarks or test cases, here are some suggestions: * Measure the performance of different DOM traversal strategies, such as `querySelector()` vs. `getElementsByTagName()` * Compare the execution times of different caching mechanisms, such as memoization vs. lazy loading * Investigate the impact of various CSS styles and layouts on JavaScript performance
Related benchmarks:
querySelector vs getElementById
querySelector vs querySelector
querySelector vs querySelector 2
getElementById vs querySelector 2
getElementById VS querySelector (simple comparison)
Comments
Confirm delete:
Do you really want to delete benchmark?