According to JS Perf, the native DOM approach with document.querySelector() is the fastest, and you send the native DOM approach to your question the slowest. Presumably, “fast” correlates with efficiency, and it seems that then id selection (like using jQuery and document.querySelector() ) is the fastest selection method.
JS Perf is linked below and is 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);
Even 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 .
Literature:
source share