• About
    • asdfasdf
      • ffdsfsdf
  • Portfolio

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

Home » Tips and Tricks » jQuery – Fast selectors using Context

jQuery – Fast selectors using Context

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



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 header div, with this we can do what we want.

Problem is, when we have pages with thousands of dom elements, cycling through them all takes a bit of time. So to speed things up, we can tell jQuery exactly where to look. This is called context. Context tells jQuery to only look in this spot.

With Context: (Correct Way)

$('#logo',$('#header')[0]).css();

Without Context: (Slow Way)

$('#logo').css()

    Share This
About the author
qrpike



Related Posts

Faster Loops in JS
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) [javascript] for ( i=0; i<=arr.length;...


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

« Faster Loops in JS
Javascript Callbacks – Async your site »
  • 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