Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

javascript - Performance of jQuery.grep vs. Array.filter

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:

  • Chrome: Even MDN shim is faster than the native method, jQuery way ahead
  • Firefox: shim a bit slower than native method, jQuery way ahead

and finally a result like i was hoping it to see in

  • Internet Explorer: native method is the fastest, then jQuery, shim is slowest (perhaps this is just the result of IEs rather weak JS-engine...)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

  1. It ignores deleted values and gaps in the array
  2. It optionally sets the execution context of the predicate function
  3. It prevents the predicate function from mutating the data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...