In a question it was discussed on how jQuery and native JS would perform against each other.
While of course the vanilla solution performs a lot faster because it does not process the whole array I proposed the usage of Array.filter
which I was pretty confident would be at least faster than $.grep
.
Surprisingly after adding it to the test I was taught a lesson: Testsuite
Edgecases of course have a different outcome.
Anyone having an idea why $.grep
is supposed to be over 3 times faster than the native method Arrray.filter
?
Edit: I modified the test to use the filter shim from MDN and the results are pretty interesting:
and finally a result like i was hoping it to see in
The grep() method finds an element and the filter() method returns elements matching a specific criteria.
To our surprise, for-loops are much faster than the Array. filter method. To be precise, the Filter method is 77% slower than for loop.
find() here will be faster as your filter() method relies on find() anyway. From your code: var allCells = table.
The difference between a built-in filter function and a for-in loop is not so obvious in this case. The build-in function is slightly faster (1.11x) than for-in loop.
As found on this blog post (which also does the same kind of tests):
If you read the documentation for
filter
, you will see why it's so much slower.
- It ignores deleted values and gaps in the array
- It optionally sets the execution context of the predicate function
- It prevents the predicate function from mutating the data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With