Carl Edwards Carl Edwards - 4 years ago 105
Javascript Question

this vs. currentValue of an iterator function

Though distinct in their own way they often times serve a similar purpose. Simply put I was wondering which of the two is more performant than the other? In general is one more preferred than the other or is it really just a matter of personal preference?

$("div").each(function(index, element) {
this.id
});





$("div").each(function(index, element) {
element.id
});

Answer Source

element reference

$("div").each(function(index, element) {
  element.id
});

appear to be "fastest" . The margin is minimal. Last test tried at chromium 49 element.id:54,990 , this.id:54,885 . Results may vary based on browser, or browser version used. Or, as @RyanO'Hara succinctly points out

generally that it doesn’t matter

jsperf http://jsperf.com/this-vs-currentvalue-of-an-iterator-function

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download