even more optimization for attachPostsToStream() to avoid as much as possible of DOM traversing

This commit is contained in:
Simon Grim 2015-05-12 04:29:48 +05:00
parent 345966effd
commit 3574c0389c

View File

@ -203,38 +203,29 @@ function attachPostsToStream(stream, posts, isPromoted) {
var streamItem = streamItems.eq(i); var streamItem = streamItems.eq(i);
streamPosts.push({item: streamItem, time: parseInt(streamItem.attr('data-time'))}); streamPosts.push({item: streamItem, time: parseInt(streamItem.attr('data-time'))});
} }
//streamPosts.sort(byTimeInDescendingOrder); // currently there is no reason to sort it yet //streamPosts.sort(byTimeInDescendingOrder); // currently here is no reason to sort it, it should be ok
for (var i = 0; i < posts.length; i++) { for (var i = 0; i < posts.length; i++) {
//console.log(posts[i]); //console.log(posts[i]);
var isAttached = false; var isAttached = false;
var intrantPost = {item: postToElem(posts[i], 'original', isPromoted), time: posts[i]['userpost']['time']}; var intrantPost = {item: postToElem(posts[i], 'original', isPromoted), time: posts[i].userpost.time};
intrantPost.item.attr('data-time', intrantPost.time); intrantPost.item.attr('data-time', intrantPost.time);
if (streamPosts.length) { if (streamPosts.length) {
// check to avoid twist duplication // check to avoid twist duplication and insert the post in timeline ordered by (you guessed) time
var streamItems = stream.children('[data-time='+intrantPost.time+']'); for (var j = 0; j < streamPosts.length; j++) {
if (streamItems.length) { if (intrantPost.time === streamPosts[j].time &&
for (var j = 0; j < streamItems.length; j++) { intrantPost.item[0].innerHTML === streamPosts[j].item[0].innerHTML) {
var streamItem = streamItems.eq(j);
if (streamItem[0].innerHTML === intrantPost.item[0].innerHTML) {
isAttached = true; isAttached = true;
console.log('appending of duplicate twist prevented'); console.log('appending of duplicate twist prevented');
break; break;
} } else if (intrantPost.time > streamPosts[j].time) {
} // this post in stream is older, so post must be inserted above
} intrantPost.item.insertBefore(streamPosts[j].item).show();
// insert the post in timeline ordered by (you guessed) time streamPosts.push(intrantPost);
if (!isAttached) { streamPosts.sort(byTimeInDescendingOrder);
for (var j = 0; j < streamPosts.length; j++) { isAttached = true;
if (intrantPost.time > streamPosts[j].time) { break;
// this post in stream is older, so post must be inserted above
intrantPost.item.insertBefore(streamPosts[j].item).show();
streamPosts.push(intrantPost);
streamPosts.sort(byTimeInDescendingOrder);
isAttached = true;
break;
}
} }
} }
} }