[英]jquery performance variable
few question, i want to know when i should use variable, if the selector will call more than one time, then i should variable that selector? 几个问题,我想知道何时应该使用变量,如果选择器将调用多次,那么我应该对那个选择器进行变量设置?
var $body = $('body');
if i have 100 jquery function, each function need $('body')
selector, local 100 time or global one time variable will faster? 如果我有100个jquery函数,则每个函数都需要
$('body')
选择器,本地100次或全局一次变量会更快?
if a function like below 如果像下面的功能
$('.link').on('click', function() { var $this =$(this); $this.addClass('a'); });
the function is finish, only call $this one time, should i need $this
? 函数完成,仅调用$ this一次,我需要
$this
吗? or just use $(this)
? 或者只是使用
$(this)
? which one is faster? 哪一个更快?
variable non-selector 可变非选择器
$('one').addClass('a').removeClass('b').html('abcd'); $('two').addClass('a').removeClass('b').html('abcd');
if i create a variable var job = addClass('a').removeClass('b').html('abcd');
如果我创建一个变量
var job = addClass('a').removeClass('b').html('abcd');
`$('one').job;
$('two').job;`
will it be faster? 会更快吗? or useless?
还是没用?
resize function variable, to made a full height div, which one will be faster? 调整功能变量的大小,以使全高div,哪个会更快?
$('#full-height').css({'height' : ($(window).height() + 'px')}); //without variable
var vH = $(window).height(); $('#full-height').css({'height' : vH + 'px'}); //with variable
var vH = $(window).height(); $('#full-height').height(vH + 'px'); //with variable and use height() method
all of above function run inside $(window).on('resize', function() {}
, so each time resize the function will run, will the variable code faster? 以上所有函数都在
$(window).on('resize', function() {}
,因此每次重新调整大小函数都将运行时,变量代码会更快吗?
when i need to variable non-selector? 当我需要改变非选择器?
thanks soooo much 非常感谢
If you use an element multiple times, saving it in a variable (a pointer for an element) will improve performance for sure, because like you said it's a query , jQuery will fetch all the DOM to find the element that you need, so saving the result of a query in a variable will be faster, and it's a good habit. 如果您多次使用元素,则将其保存在变量(元素的指针)中肯定会提高性能,因为就像您说的是一个查询一样 ,jQuery会获取所有DOM来查找所需的元素,因此保存查询变量的结果会更快,这是个好习惯。
So using $this
instead of $(this)
will improve performance too. 因此,使用
$this
代替$(this)
也会提高性能。
*For the Question 3 *对于问题3
You can't do job = addClass...
because addClass is not a global functions! 您无法执行
job = addClass...
因为addClass 不是全局函数! it's a jQuery object method! 这是一个jQuery对象方法! so you can use it only with a jQuery object/element
$jqObject.addClass...
因此您只能将其与jQuery对象/元素
$jqObject.addClass...
*Question 4 *问题4
Like I said if you are using a result of a method multiple times in the same function , you should put it in a variable, but in your example you are using the $(window).height();
就像我说过的,如果您在同一个函数中多次使用方法的结果,则应将其放在变量中,但在您的示例中,您将使用
$(window).height();
only one time, so puting it in variable is useless but it's a good habit for a clean code . 只有一次,因此将其放入变量中是没有用的,但这是编写干净代码的好习惯。 and it will not affect performance.
并且不会影响性能。
So in simple words: if you are using a result of a method multiple times in the same context, saving it in a variable will improve performance. 简而言之:如果在同一个上下文中多次使用方法的结果,则将其保存在变量中将提高性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.