jQuery链接性能

jQuery链接性能,jquery,performance,chaining,Jquery,Performance,Chaining,这些速度相等吗 $(this).attr("date",date); $(this).attr("date_start",date_start); $(this).attr("heure_start",heure_start); 或 即使第二个更快,将其分开编写以使代码更具可读性是否更好?不,两者在速度上并不相同 $(this)每次构建一个新的jQuery对象。这可能是一个复杂的操作,具体取决于此 所以第二种形式更快 请注意,为了便于阅读,您可以将其编写为 $(this) .attr(

这些速度相等吗

$(this).attr("date",date);
$(this).attr("date_start",date_start);
$(this).attr("heure_start",heure_start);


即使第二个更快,将其分开编写以使代码更具可读性是否更好?

不,两者在速度上并不相同

$(this)
每次构建一个新的jQuery对象。这可能是一个复杂的操作,具体取决于此

所以第二种形式更快

请注意,为了便于阅读,您可以将其编写为

$(this)
    .attr("date",date)
    .attr("date_start",date_start)
    .attr("heure_start",heure_start);
如果由于中间有其他代码行而无法链接操作,也可以缓存对象。通常是这样的:

var $this = $(this);
$this.attr("date", date);
$this.attr("date_start", date_start);
$this.attr("heure_start", heure_start);
还要注意,可以将映射作为参数:

$(this).attr({
    date: date,
    date_start: date_start,
    heure_start: heure_start
});

为了便于阅读,您可以将行拆分为

$(this)
    .attr("date",date)
    .attr("date_start",date_start)
    .attr("heure_start",heure_start);

我知道这应该是一个评论,但间距作为一个评论是没有意义的。

这是一个完整的解释!
$(this)
    .attr("date",date)
    .attr("date_start",date_start)
    .attr("heure_start",heure_start);