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:
- console.time("1");
- for(var i=0; i<2000; i++) {
- $("#jq-intro");
- }
- console.timeEnd("1");
- console.time("2");
- for(var i=0; i<2000; i++) {
- $("#jq-content");
- $("#jq-intro");
- }
- console.timeEnd("2");
- console.time("3");
- for(var i=0; i<2000; i++) {
- $("#jq-intro", $("#jq-content"));
- }
- console.timeEnd("3");
On my slow test computer I see the following results: