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

(Node.js+express+MongoDB), how to OPTIMIZE an frequently-used variable?

(Node.js+express+MongoDB), how to OPTIMIZE an frequently-used variable?


I'm building a dynamic website using Node.js+express+MongoDB

And there're some numbers that exist in almost every page,(like the amount of users/pages/articles of the site, showing in the footer)

since they exist in every page, I think they will slow down my site, if I call it every time like before rendering just like an not such useful variable.

Am I right?

1.if so, how to Optimize this prob? use dynamicHelpers? how to do that? is there any examples?

2.which one is better? put them in DB or in CONFIG file?

Problem courtesy of: Nano WANG


Since you are using mongo, you can put all of these keys inside an index.

If your doc looks like this:

doc = {
    'users': 50,
    'page': 10,
    'articles': 20

simply do: {
    'users': 1,
    'page': 1,
    'articles': 1

Then make sure you only return these keys.

If these are in an index, Mongo will read these values directly from memory and not from disk.

If you want to query this by something like an _id instead, simply add the _id as the first part of the index, and include the rest of the items in the index.

Solution courtesy of: Spencer


View additional discussion.

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

Share the post

(Node.js+express+MongoDB), how to OPTIMIZE an frequently-used variable?


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription