Lazy / Underscore / Lodash performance comparison in a daily use case

In the world of the "performance libraries", Underscore.js is the oldest one (5 years), and also the most well-known. Other similar standard libraries, such as Lodash have replicated the Underscore API, but by providing in most cases better performances over Underscore. Beside Underscore and Lodash, it exists a number of less well-known standard libraries, which all performs usually less good than Lodash.

New kind of libraries

More recently, lazy evaluation libraries as a workaround to Underscore have appeared. These libraries do the computing only when required, which can represent in some situation a huge difference. This is this difference I will try to highligh in a real use case with Lazy.js.

Real use case

With real use case, I want to differenciate this test from a number of benchmark I'm seeing around (lazy.js website, another test, still another test). While all these benchmarks are accurate, they usually don't represent a real use case: you probably don't filter your set of data 1 million of time per second, 1 time is likely enough - and that it happens in 1 micro second or in 5 micro second, none of your users will notice the difference.

However, let's consider a more realistic situation: you get from your Database (backend or local like IndexedDB or SQLite) a huge set of data: an array of a few hundred thousands rows. You have a UI on which you have implemented different filters, and you want to filter in real time this set (you could do it natively with SQLite, but not with IndexedDB nor with Backend Data). So which library performs really the best?

Result

I have setup a small demo, for that. First, 500.000 rows are randomly created, and each of these lines is an object with 14 properties (symbolising columns from the DB).

Then, 2 filters (filter1 and filter2) are created filtering all records, based on the properties a and b, and starting with the letter f.

First we can see that Underscore is not that slow compared to Lodash (actually, there is no difference).

Then there are 2 different setups with Lazy. One, which returns all the results, and another one which returns just the 10 first matching records. Indeed, in a real use case, we don't display thousands of records on the UI, but probably the first 10 or 20 records, which can fit on the screen. This is the real difference with a standard library. Unlike Underscore or Lodash, it's not necessary to process all the records if you just want to extract some of them. And the good thing is that even if you are returning all the records, Lazy.js performs the same or sometimes even slightly better than Lodash/Underscore (test the demo).

Conclusion

This is just one example, but this is also a situation, which requires the use of such libraries. If you have a set of tens or hundreds of records, the performance of the library is really not critical.

comments powered by Disqus