According to JS Perf, the native DOM approach, with document.querySelector()
is fastest, and the native DOM approach you post in your question the slowest. Presumably 'fast' correlates to efficient, and it seems, then, that selecting by id
(as both the jQuery and document.querySelector()
approaches use) is the fastest method of selection.
The JS Perf is linked below, and based on the following HTML:
<form action="#" method="post" name="trek">
<fieldset>
<label for="input1">
Input One:
</label>
<input id="input1" value="An input, Jim; but just as we know it..." />
</fieldset>
<fieldset>
<label for="input2">
Input Two:
</label>
<input id="input2" value="'Beam me up, Scotty!' attributed to, but never said, in Star Trek."
/>
</fieldset>
</form>
Native DOM:
var input1 = document.trek.input1.value,
input2 = document.trek.input2.value;
console.log(input1, input2);
More DOM:
var input1 = document.getElementById('input1').value,
input2 = document.getElementById('input2').value;
console.log(input1, input2);
Yet more DOM, d.qS:
var input1 = document.querySelector('#input1').value,
input2 = document.querySelector('#input2').value;
console.log(input1, input2);
And jQuery:
var input1 = $('#input1').val(),
input2 = $('#input2').val();
console.log(input1, input2);
JS Perf.
References: