I did a test with few iterations to test efficiency of Document.querySelector
and Element.querySelector
.
Markup:
<form>
<input type="text" />
</form>
Script:
Querying with Document.querySelector
begin = performance.now();
var
i = 0,
iterations = 999999;
for ( i; i < iterations; i++ )
{
element = document.querySelector('[type="text"]');
}
end = performance.now();
firstResult = end - begin;
Querying with Element.querySelector
begin = performance.now();
var
i = 0,
iterations = 999999,
form = document.querySelector('form');
for ( i; i < iterations; i++ )
{
element = form.querySelector('[type="text"]');
}
end = performance.now();
secondResult = end - begin;
Log:
console.log( firstResult ); // 703.7450000001118
console.log( secondResult ); // 1088.3349999999627
The log is amazing for me because i think that Element.querySelector
query only on nodes that is a descendant of the element and Document.querySelector
query on all nodes of current document, right?
Why get this result?