Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Best approach to data filtering

Best approach to data filtering

Problem

I'm building a little library applications to have a visual catalogue of my programming ebooks. By now, I've added some of my ebooks info into a ko.observableArray in my BooksViewModel.js file. Later, I'll be implementing a NodeJS applications with all the data saved in a MongooseDB and access them from there, but by now, I'm just experimenting directly from Knockout.js.

By default, my library shows all the books I added, desorganized, so I'm looking forward to implement "categories" by Language. Every book object contains a language attribute. I want to filter the books showed by language but I'm a little bit confused on how will be the best way to do this.

The books on the array are not organized, they are all dropped there.. some talks about javascript, other C and so on. At first I thought about creating a separated array for each language, and then implemeting a method in the ViewModel to select the corresponding array of the language you requested.

Later, I would implementa NodeJS API, to get them by language, lets say:

GET /languages/C // will get a json corresponding all the books that talks about C

The ViewModel could contain a method:

self.findByLanguage = function(lang) {
   self.books = // GET /languages/:lang
};

But that would query the database every time. I guess is better to load the whole books json first, saved all of them to an array on the client side, and then filter them. That way only one request would be made. I could have a global array containing all the books, and then implement the filter with ko.utils.ArrayFilter.

What do you guys think will be the best approach? Maybe there is a better way.

Thanks in advance!

Problem courtesy of: jviotti

Solution

If "my programming ebooks" means this application is for you only, there's a trivial difference between querying all and only the selected few books as the database load will generally be close to zero in either of these cases. The number of books would be a few hundred perhaps.

But wait, what's the actual benefits from loading them all at once?

Upsides of storing the whole list client-side

  • If you are always looking at most of the categories will save you some milliseconds of database load and all bandwith just from changing categories.

Downsides

  • Bandwith usage is awful, initial page loading is slower giving you plenty of books you don't want or need.
  • The database system you're using is having speed as important optimization factor. Add an index on language and querying should be done in no time, anyhow. For the time you're using arrays as data source, this might not show in comparison to 'just sending the whole array'.
  • Opening the page in multiple windows/browsers/on multiple pcs will require you to syncrhonize all changes to all clients. If you don't do this, you'll have old objects until you reload the page which is exactly what you should avoid if having the list client-side.

If you're planning to run this on your local computer or within your local network, speed should be a trivial issue, so why not let the database do the work? If you're not and speed is an issue, I would personally value "I can load category X pretty fast" over "Initial page loading is slow, but it's fast once everything's loaded".

Solution courtesy of: mabako

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Best approach to data filtering

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×