query by id - ignore context

query by id - ignore context

When selecting elements jQuery provides an optional 2nd argument (context) which limits the search to a specific node.  Adding this argument usually speeds up queries, but when the query is by id adding the context actually siginificantly slows down the query.

As a test, go here:


and run this script in Firebug:

  1. console.time("1");
  2. for(var i=0; i<2000; i++) {
  3. $("#jq-intro");
  4. }
  5. console.timeEnd("1");

  6. console.time("2");
  7. for(var i=0; i<2000; i++) {
  8. $("#jq-content");
  9. $("#jq-intro");
  10. }
  11. console.timeEnd("2");

  12. console.time("3");
  13. for(var i=0; i<2000; i++) {
  14. $("#jq-intro", $("#jq-content"));
  15. }
  16. console.timeEnd("3");

On my slow test computer I see the following results: