I measured the execution times of those two functions:
The execution of following methods have been measured using Chrome Profiles tool:
// jQuery GREP function
function alternative1(words, wordToTest) {
return $.grep(words, function(word) {
return wordToTest.indexOf(word) != -1;
});
}
// Native javascript FILTER function
function alternative2(words, wordToTest) {
return words.filter(function(word) {
return wordToTest.indexOf(word) != -1;
});
}
Array of words
was constructed of 1 million randomly generated strings. Each method was run 20 times. On my surprise jQuery
grep function was faster.
Execution times (20 executions):
You can repeate measurings on this jsFidle - it will take some time to execute so be patient.
Is there any explanation why jQuery grep function is faster then native JavaScript filter function?
PS: This questions was inspired by this answer.