Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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中每个过滤器和过滤器之间的区别?_Javascript_Performance - Fatal编程技术网

javascript中每个过滤器和过滤器之间的区别?

javascript中每个过滤器和过滤器之间的区别?,javascript,performance,Javascript,Performance,我想知道Array.prototype.each和Array.prototype.filter中的哪些函数在javascript中速度快? 我所知道的不同之处在于,每个过滤器都可以通过返回false来停止,而过滤器不能通过返回false来停止。 除了这个区别,还有其他区别吗? 如果其中哪一个有索引?: 确定像a[*]==b[*]这样的数组的所有成员是否满足指定的测试。 (完全真理) : 正如测试x存在于[*] (如果至少有一个匹配,则为真值)函数执行完全不同的操作 将创建一个数组,其中包含与回调

我想知道
Array.prototype.each
Array.prototype.filter
中的哪些函数在javascript中速度快? 我所知道的不同之处在于,每个过滤器都可以通过返回false来停止,而过滤器不能通过返回false来停止。 除了这个区别,还有其他区别吗? 如果其中哪一个有索引?

确定像
a[*]==b[*]
这样的数组的所有成员是否满足指定的测试。 (完全真理

:

正如测试
x存在于[*]

如果至少有一个匹配,则为真值)

函数执行完全不同的操作

将创建一个数组,其中包含与回调中的条件匹配的所有元素

function isBigEnough(element) {
  return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]
function isBigEnough(element, index, array) {
  return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true
如果数组中的每个元素都与回调中的条件匹配,则返回true

function isBigEnough(element) {
  return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]
function isBigEnough(element, index, array) {
  return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true

过滤器速度更快。检查:@NabRaj,jsperf实际上并没有测试所讨论的数组原型方法。好的。我的感觉是,一种方法对每件事都进行了测试,而另一种方法在找到匹配项后立即返回。我相信我们不能根据他们的速度来判断,这违背了他们的意思。