Issue
I was wondering which of the functions among Array.prototype.every
and Array.prototype.filter
are fast in javascript?
The difference that I know is that every can be stopped by returning false and filter cannot stop by returning false.
Apart from this difference is there any other?
And if which one among this has indexing?
Solution
The functions do completely different things.
Array.prototype.filter
will create an array of all the elements matching your condition in the callback
function isBigEnough(element) {
return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]
Array.prototype.every
will return true if every element in the array matches your condition in the callback
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
Answered By – migvill
Answer Checked By – Mary Flores (BugsFixing Volunteer)