Skip to content

for loop optimisation #2

@sakex

Description

@sakex

You could optimize the for loop by hoisting your iterator and posts.length. It would take much less time if you didn't have to check posts.length every time.

for(let i=0; i<posts.length; i++) {
        posts[i].upvotes = (posts[i].upvotes +posts[i].commentCount)/divider;
}

Would take less time implemented this way:

var i;
const len = posts.length;

for(i=0; i<len; ++i) {
        avg += (posts[i].upvotes +posts[i].downvotes +posts[i].commentCount)/3;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions