Well, I think I know the answer to this question, wanting to confirm. Therefore, I have a selector that was used only once, but it was used inside a function that was called several times. In terms of performance, since this selector is re-scanned — every time a function is called, is it possible (albeit slightly) to cache the selector?
In other words, below ...
function testFunction() {
alert($("#input").val())
}
$("#a").click(function() {
testFunction()
})
$("#b").click(function() {
testFunction()
})
$("#c").click(function() {
testFunction()
})
... not as effective as below
input = $("#input")
function testFunction() {
alert(input.val())
}
$("#a").click(function() {
testFunction()
})
$("#b").click(function() {
testFunction()
})
$("#c").click(function() {
testFunction()
})
source
share