Take the following two ways of removing an array of elements from the DOM using jQuery:
var collection = [...]; // An array of jQuery DOM objects
// Using jQuery iteration
$(collection).each(function(index, element) { element.remove(); });
// Or as pointed out by Barmar
$(collection).remove();
// Using native iteration
collection.forEach(function(element) { element.remove(); });
Is there any real difference operationally? I'd expect, unless the browser interpreter/compiler is clever enough, that there would be additional unnecessary overhead with the former method, albeit probably minor if the array is small.
collection.forEach(function(element) {element.parentNode.removeChild(element);})
is a fairer comparison.$(selector).each()
calls the function with the element as the argument, not the jQuery object.$.each(array, function...)
, not$(array).each(function...)