• About
    • asdfasdf
      • ffdsfsdf
  • Portfolio

  • Web Development
  • SEO Techniques
  • Tips and Tricks
  • Site News

Home » Tips and Tricks » Faster Loops in JS

Faster Loops in JS

Posted by: qrpike    Tags:  javascript, JQuery, loops, optimization    Posted date:  November 21, 2011  |  No comment



Loops in javascript are very common and we use them a lot to cycle through arrays. Problem is most of the time we do them the wrong way.

For example: (most common use of a loop)

for ( i=0; i<=arr.length; i++ ){
       console.log(arr[i]);
}

Problem is that we are checking the length of the array everytime we cycle through, which is bad. We only need to check the length of the array once, and then continue through. Also While loops tend to be faster than for loops. So to speed things up, we should use this:

var x = arr.length;
while(x--){
       console.log(arr[x]);
}

If you noticed, we are also going in reverse so that we are not checking against a length, we are only checking that we are taking 1 integer away from the (x) variable, when we can no longer do that, we stop.

If you must go in the correct order, the following is the next quickest loop:

var i=0, len=arr.length;
while (i<len) {
    console.log(arr[i]);
}

A good article on benchmarking loops in JS:
http://blogs.oracle.com/greimer/entry/best_way_to_code_a


    Share This
About the author
qrpike



Related Posts

jQuery – Fast selectors using Context
One of the biggest performance areas of javascript and jQuery is the selectors. Selectors are basically what we tell jQuery to select in the DOM that we want to manipulate. For example $('#header') would select the entire...


Is node.js fast? yes!
I was doing some research a few days ago and came across some valuable information dealing with Node.js. As I already knew node.js is built on top of Googles javascript engine called, V8. I went into depth trying to find...


Javascript – Make a variable inside of a function inside of a object global
Okay, the title is pretty complicated but this is useful! lol Today I had a issue with accessing a variable inside of a object, inside of a function. Let me show you: [javascript] function someobject(){ this.cat...


Wanna say something?





  Cancel Reply

« Is node.js fast? yes!
jQuery – Fast selectors using Context »
  • Recent Posts

    • nginx init.d start script
      #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig:...
    • Install nginx on CentOS Min.
      First you will need to install a compiler: yum install gcc-c++ Then install the Perc...
    • MYSQL Connection Pool in Node.js
      So, one of the cool advantages of Node.JS (over PHP for example) is the ability to pool...
    • Install FTP and Add User on Amazon EC2
      First we need to install very secure ftp demon yum install vsftpd Once its installed...
    • Install LAMP on Amazon EC2
      If you create a new 64-bit Amazon AMI on their EC2 system, this is the easiest way to get LAMP...



 

 
Copyright © 2010 Peerapong. Remove this once after purchase from the ThemeForest.net