JavaScript faster for loops


Assuming you want to traverse through an array using for-loop

var array = [...........]

Slow Way

array.forEach(function(x){
    // TODO: Do Something
});

Fast Way

for(var i = 0; i < array.length; i++){
    // TODO: Do Something
}

Faster Way

for(var i = 0, len = array.length; i < len; i++){
    // TODO: Do Something
}

Reference: https://blogs.oracle.com/greimer/entry/best_way_to_code_a

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s