<context-provider id="provider">
<div>
<div></div>
<div>
<span>
<span>
<span>abc</span>
</span>
def
<span>
<span id="foo"></span>
</span>
</span>
ghi
</div>
</div>
</context-provider>
var context = document.getElementById('provider');
var element = document.getElementById('foo');
var SimpleContextEvent = class SimpleContextEvent extends Event {
data = null;
callback = null;
constructor(callback) {
super('context-request', {bubbles: true, composed: true});
this.callback = callback;
}
};
context.addEventListener('context-request', event => {
if (event.callback) {
event.callback(context);
} else {
event.data = context;
}
event.stopPropagation();
});
var i = 5000;
while (i--) {
let ctx = element;
ctx = ctx.closest('context-provider');
if (ctx !== context) {
throw new Error('mismatch');
}
}
var i = 5000;
while (i--) {
let ctx = element;
while (ctx) {
if (ctx.tagName === 'CONTEXT-PROVIDER') {
break;
}
ctx = ctx.parentNode;
}
if (ctx !== context) {
throw new Error('mismatch');
}
}
var i = 5000;
while (i--) {
let ctxEvent = new SimpleContextEvent();
element.dispatchEvent(ctxEvent);
let ctx = ctxEvent.data;
if (ctx !== context) {
throw new Error('mismatch');
}
}
var i = 5000;
var ctx;
var ctxEvent = new SimpleContextEvent(reply => {ctx = reply;});
while (i--) {
ctx = null;
element.dispatchEvent(ctxEvent);
if (ctx !== context) {
throw new Error('mismatch');
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
closest with element selector | |
while loop + tagName check | |
event propagation | |
event propagation with callback |
Test name | Executions per second |
---|---|
closest with element selector | 468.3 Ops/sec |
while loop + tagName check | 409.0 Ops/sec |
event propagation | 81.5 Ops/sec |
event propagation with callback | 122.7 Ops/sec |
Let's dive into the world of microbenchmarks.
Benchmark Overview
The provided JSON represents a JavaScript benchmarking test case, which measures the performance of different approaches to get a contextual element (in this case, an element with the context-provider
class) using various methods. The test is designed to compare the speed and efficiency of these approaches across multiple browsers and devices.
Test Cases
There are four individual test cases:
closest()
method on the element
variable to find the nearest ancestor with the context-provider
class. It then checks if this element is equal to the expected context
value.element
down, checking each parent node's tag name until it finds one with the context-provider
class. If the final result does not match the expected context
, an error is thrown.SimpleContextEvent
) on the element
and checks if the resulting data matches the expected context
value after stopping event propagation with event.stopPropagation()
.SimpleContextEvent
constructor) to handle the dispatched event.Library and Special JS Features
The custom library used here is called SimpleContextEvent
, which extends the built-in Event
class. It provides additional functionality for creating events with custom data and callbacks. The context-provider
element is not a standard HTML element, but rather a custom class applied to an element.
Options Compared
Here's a brief summary of each test case:
closest()
method, which is likely to be the fastest approach due to its optimized implementation.Pros and Cons
Here's a brief pros-and-cons summary for each test case:
Other Alternatives
Some alternative approaches that might be worth considering:
jQuery
or React
for event handling and DOM manipulation.context-provider
) for faster element selection.getElementsByClassName()
or querySelectorAll()
, which are optimized for performance.Keep in mind that the choice of approach depends on your specific use case, target browsers, and requirements. These microbenchmarks should provide a general idea of each method's relative performance but might not be representative of all scenarios.