JQuery $ .each vs JavaScript.forEach

Take two ways to remove 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 online? I would expect if the browser interpreter / compiler is not smart enough that there will be an extra superfluous overhead resource with the previous method, although it is probably insignificant if the array is small.

+12
source share
4 answers

From a practical point of view, this is a bit vague, but here's how $ .each is implemented

https://github.com/jquery/jquery/blob/master/src/core.js#L296

[].forEach()most likely will be implemented in native code for each browser.

. , JQuery , ,

+5

, .

, :

1) Array.forEach() , . jQuery .

2) jQuery.each() . , "some()" "every()" js:

Array.forEach, , ?

+3

Perf test

forEach , , jquery, , .

+1

Array.forEach - , Javascript.

jQuery collection.each - , jQuery . jQuery .

, jQuery, Array.forEach - .

-2

Source: https://habr.com/ru/post/1669522/


All Articles