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 7887999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:43:30+00:00 2026-06-03T05:43:30+00:00

People here often suggest to cache the jQuery object created from a DOM element,

  • 0

People here often suggest to cache the jQuery object created from a DOM element, like with this code:

$('#container input').each(function() {
    $(this).addClass('fooClass');
    $(this).attr('data-bar', "bar");
    $(this).css('background-color', 'red');
});
  • Does caching the jQuery object really improve the performance of our code?
  • What happens “behind the scenes” when you pass a DOM element to the jQuery constructor?
  • 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-06-03T05:43:31+00:00Added an answer on June 3, 2026 at 5:43 am

    In the jQuery tag info this warning appears:

    The jQuery function $() is expensive. Calling it repeatedly is extremely inefficient.

    Well… that is true only for string selectors, which get parsed with regex to find out what they are:

    quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/
    

    Then if the string is a selector (other than id), jQuery traverses the DOM to find a match with its expensive find function:

    } else if ( !context || context.jquery ) {
        return ( context || rootjQuery ).find( selector );
    }
    

    So yes it’s expensive, but that is only true for selectors!

    If we pass a DOMElement, the only action jQuery does is saving the DOMElement parameter as the context of the newly created jQuery object and setting the length of the context to 1:

    // Handle $(DOMElement)
    if ( selector.nodeType ) {
        this.context = this[0] = selector; // Selector here is a DOMElement
        this.length = 1;
        return this;
    }
    

    I did some tests with jsPerf, and I found that indeed caching the jQuery object has only a little effect:

    Bar chart, described below

    In Chrome it’s only 7% slower. (In IE it’s a little bit more significant: 12%.)

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

Sidebar

Related Questions

I often hear around here from test driven development people that having a function
I often see people asking XML/XSLT related questions here that root in the inability
Like many people here, I started my programming experience with the good ol' green
It seems like a lot of people here and on many programmer wikis/blogs/ect. elsewhere
Some time ago, people here helped me very well with code. Now I'm tasked
Here is the thing. Right now I have this e-commerce web site where people
I often here people talk about Big O which measures algorithms against each other
I know people often get told off here for using regexes to fiddle with
I often see people talking that the GIL is per Python Interpreter (even here
One often needs to read from memory one byte at a time, like in

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.