Is it good to empty an array on focusout?
var arr = [];
$("#automobil").focus(function() {
$.getJSON("auto.json", function(data) {
$.each(data, function(key, value) {
if ($.inArray(value.name, arr) === -1) {
arr.push(value.name)
}
})
});
}).autocomplete({
source: function (request, response) {
var term = $.ui.autocomplete.escapeRegex(request.term)
, startsWithMatcher = new RegExp("^" + term, "i")
, startsWith = $.grep(arr, function(value) {
return startsWithMatcher.test(value.label || value.value || value);
})
, containsMatcher = new RegExp(term, "i")
, contains = $.grep(arr, function (value) {
return $.inArray(value, startsWith) < 0 &&
containsMatcher.test(value.label || value.value || value);
});
response(startsWith.concat(contains));
}
}).focusout(function() {
arr = [];
});
In this case, auto.json isn't big, so arr[]
isn't big either. But, in "real world examples", there can be large amounts of data, so the array needs to be empty on focusout (after the job is done), because of resources.
arr=null;
is a good practice. \$\endgroup\$