Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6216997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:20:52+00:00 2026-05-24T07:20:52+00:00

Using jQuery, if I am writing code like this $(‘blah’).doSomething(); //bunch of code $(‘blah’).doSomethingElse();

  • 0

Using jQuery, if I am writing code like this

$('blah').doSomething(); 
//bunch of code
$('blah').doSomethingElse();
//bunch of code
$('blah').doOtherStuff();

Is a new jQuery object being created each time I say $(‘blah’) ?

If so, would it reduce object creation overhead to do something like this instead:

var blah = $('blah');
blah.doSomething(); 
//bunch of code
blah.doSomethingElse();
//bunch of code
blah.doOtherStuff();

Make sense?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:20:54+00:00Added an answer on May 24, 2026 at 7:20 am

    Absolutely correct!

    Why Cache

    Another benefit of caching is code maintainability. If the selector is only in one place, changing it only needs to be done in one place. Trust me that makes life easier when maintaining code.

    Chain

    Although you can go one step further (where relevant), and chain your calls:

    $('blah')
    .doSomething()
    .doSomethingElse()
    .doOtherStuff();
    

    This is slightly better for 2 reasons. Firstly, you’re not using the extra variable, so you save a tiny amount of memory. Secondly, the language doesn’t perform lookups for each identifier use, so it’s quicker.

    So, why do people still [over]use $()

    One reason people use lots of $() is because they don’t know any better.

    Another reason is because element selection used to be slower. But as with most things in the browser, querying objects with selectors is fast being optimised (document.querySelectorAll), which means it’s not such a big deal to cache or not to cache, so maybe with that in mind they allow themselves not to cache.

    Finally, there are benchmarks hanging around (one two from a quick google) that try to claim that it doesn’t matter or is maybe even faster not to cache. The problem with most of these benchmarks and why I suggest you be very wary about drawing conclusions from them, is that the DOM in the example is not real-world; it’s overly simplistic. Secondly, the selectors are simplistic as well. So of course querying will be lightning fast and caching won’t make a big difference, but I don’t find them at all conclusive.

    That said, if your example is similar to those of the benchmarks, then maybe you can draw your own circumstantial conclusions, and caching might just be a nuisance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JQuery. I'm writing this as HTML code: <li id=toggle> <a id=open class=open
I am new to JQuery, and I am attempting writing code using PHP, HTML,
Using JQuery, is there a more efficient way of writing this code? See below
Lately I've been writing some JS code using jQuery and JavaScript as it is
I am writing my code like this: function updates_cb() { in a while loop
I'm writing a mobile web application using JQuery Mobile, JQuery, Javascript. This is my
I'm diving into writing a mobile app with jQuery Mobile/PhoneGap. I'm using this sample
I am writing a form using jQuery and encounter some difficulties. My form works
I am writing a site using jquery that repeatedly calls $(window).width() and $(window).height() to
I'm writing a semi-generic form plugin using jQuery in order to speed up the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.