Why Mongoose sort is slower than native javascript sort? Why Mongoose sort is slower than native javascript sort? mongoose mongoose

Why Mongoose sort is slower than native javascript sort?


As @JuanMendes said in the comment, the first one does sort and then apply limit, which means MongoDB sorts all billions of documents and returns you 24 documents. In the second one, you are getting only 24 documents from MongoDB and sorting them using javascript's sort. So the difference is not between native javascript sorting and MongoDB sorting. The difference is between sorting 24 documents and sorting billions of documents

The order of sort and limit does not matter. MongoDB always does sort before applying limit. See this