Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQuery中,哪种更改JS元素集合的方式更快_Javascript_Jquery_Performance - Fatal编程技术网

Javascript 在jQuery中,哪种更改JS元素集合的方式更快

Javascript 在jQuery中,哪种更改JS元素集合的方式更快,javascript,jquery,performance,Javascript,Jquery,Performance,我有一个全选按钮,我想实现。哪条路更快 $('.check-box').prop('disabled', false).trigger('change'); 或 在UI中,它们做同样的事情。 第二个似乎不仅按类名选择,而且还选择每个元素,这是一个O(MN)操作。但是第一种方法是否可能正是第二种方法在幕后所做的事情(尽管这会使第一种方法更有利,因为它的代码更少,加载时间更快)?大多数jQuery方法都有一个内部每个,基本上看起来像: // $.fn is where chainable meth

我有一个全选按钮,我想实现。哪条路更快

$('.check-box').prop('disabled', false).trigger('change');

在UI中,它们做同样的事情。
第二个似乎不仅按类名选择,而且还选择每个元素,这是一个O(MN)操作。但是第一种方法是否可能正是第二种方法在幕后所做的事情(尽管这会使第一种方法更有利,因为它的代码更少,加载时间更快)?

大多数jQuery方法都有一个内部
每个
,基本上看起来像:

// $.fn is where chainable methods reside 
$.fn.methodName = function(){

   // "this" is the jQuery object that contains collection of elements 
   // returned by either $(selector) or prior method in chain

   // return "this" for chaining and loop over all elements in collection
   return this.each(function(){
       // "this" is individual element instance

       // do same thing to each element here
   });
}

这就是
$('.check box').prop('disabled',false.).trigger('change')
将影响所有元素
class=“check box”

这是一个简单的JavaScript等价物,它比Chrome中的任何jQuery jQuery都要快,但比Firefox中的jQuery慢(正如charlietfl所指出的)

  • 分类收集

  • 遍历每个

  • 更改禁用的
    属性/属性

  • 单击每个节点

第一个jQuery示例比第二个jQuery示例快。最明显的区别是
.each()
方法多了一步<代码>$('.check box')
与所有jQuery对象一样,有一种内部方式来迭代自身

演示1
document.querySelectorAll('.check box').forEach(节点=>{
node.disabled=false;
node.click();
});


可能非常相似,也不重要。。。但你们可以做jsperf测试。你们要处理多少元素?令人惊讶的是,你们只需在firefox中运行jsperf两次……第一个jQuery方法是3个方法中最快的,速度提高了几个百分点。更改属性而不是属性可能会更快
node.disabled=false
在这种情况下
getElementsByClassName
可能会更快?另外,
forEach
与IE 6不兼容-8@charlietfl确实,您认为这是V8优化吗?@DylanCzenski我认为您可能是对的,区别在于
getElementsByClassName
返回一个实时HTMLCollection,而
querySelectorAll
可能返回一个“静态”节点列表?
// $.fn is where chainable methods reside 
$.fn.methodName = function(){

   // "this" is the jQuery object that contains collection of elements 
   // returned by either $(selector) or prior method in chain

   // return "this" for chaining and loop over all elements in collection
   return this.each(function(){
       // "this" is individual element instance

       // do same thing to each element here
   });
}