May 3, 2012

COUNT() function each time, or store the value and increment it by one?

Question by john

I have a database with a user ‘votes’ table and a ‘user’ table. I’m thinking the database will get quite big in a small amount of time so i want to use the most efficient method.

I’m thinking i can either COUNT() the amount of votes with a WHERE statement from the ‘votes’ table every time, or i can store the score in the ‘user’ table and just increment it by 1 every time a vote is added.

Which would be best/quickest, and/or are there any other ways of doing this?

Answer by Starx

If you are thinking of the best way to do it. You have to look into optimizing and caching a lot.

I would say,
Create a column on the user tables to store cached score, but maintain the score on the separate table.

Whenever score changes operate of scores table and trigger an update on user’s table with the latest score result.

Doing this, you have extendability in your score data to, kind of like what stackoverflow uses for votes.

April 1, 2012

Which should I use? (performance)

Question by Yim

I want to know a simple thing:

when setting up a style that is inherited by all its children, is it recommended to be most specific?

Structure: html > body > parent_content > wrapper > p

I want to apply a style to p but respecting these:

  • I don’t care having parent_content or wrapper having the style
  • I do care changing the html or body style (or all p)

So what should I use?

#parent_content{
    color:#555;
}

#parent_content p{
    color:#555;
}

#wrapper{
    color:#555;
}

#wrapper p{
    color:#555;
}

/*...etc...*/

Also, some links to tutorials about this would be great

Answer by Starx

In the matter of specificity, give an id to the p and use

#paragraphid {}

But the answer depends what actually are your need. I will break down your code

#parent_content{
    color:#555;
}

Will apply the color the text inside and may be inside its children also

#parent_content p{
    color:#555;
}

Will apply the color to all the p inside #parent_content and its children

#wrapper{
    color:#555;
}

Will apply the color to all the text inside it, and of its children

March 2, 2012

Fastest way to build this string

Question by tester

say I have an array of objects with html strings inside (there are other things, but i’m specifically focusing on the html property of each object. e.g.

var items = [{
    html: '<div>test</div>'
}, {
    html: '<div>test</div>'
}, {
    html: '<div>test</div>'
}];

I need to build a string using all of these strings and I need them in the same order they’re given to me, so a reverse while loop is out.

is there anything faster at building the html than the following?

var html = [];
for (var i = 0, itemLen = items.length; i < itemLen; i++) {
    html.push(items[i].html)
}
output.innerHTML = html.join('');

Answer by user1150525

faster would be:

var html = '';
for (var i = 0, itemLen = items.length; i < itemLen; ++i)
    html += items[i].html;
output.innerHTML = html;

Edit:

This is faster:

var html = '';
for (var i = 0, itemLen = items.length; i < itemLen; html += items[i++].html);

Answer by Starx

This is much faster than yours

var html = '';
for (var i = 0, itemLen = items.length; i < itemLen; i++) {
    html += items[i].html;
}
output.innerHTML = html;
...

Please fill the form - I will response as fast as I can!