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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:35:36+00:00 2026-06-14T22:35:36+00:00

Pretty basic question. I want to know if jQuery or raw JavaScript are smart

  • 0

Pretty basic question.

I want to know if jQuery or raw JavaScript are smart enough to precompile, or cache variable expressions in functions, or selectors, and if not, are there any reasons that would stop them doing this?

e.g.

$('#whatever').on('click', function(){
    $('#template').click();
});

var n = 0;

for(var i = 0; i < 20; i++){
    n = 1 + 1;
    $('#whatever').click();
}

console.log(n);

Are we going to search the dom 20 times for the $('#template') element? or are the library/engine smart enough to store a pointer?

And, are we going to computer 1+1 2 times, or is that expression cached or precompiled?


Obviously these are really dumb examples, but they get the point across.

Yes, no one is going to have the line (hopefully) n = 1 + 1 in their code.

And we could change the code to have `var template = $(‘#template’); before the click handler if it doesn’t deal with it itself, but I am wondering do we/ why we have to do that?

  • 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-14T22:35:37+00:00Added an answer on June 14, 2026 at 10:35 pm

    First off, jQuery is javascript. jQuery does not and cannot add features to JavaScript as a language.

    With that being said, JavaScript is ECMAScript, and also implemented differently in every browser. It would be very difficult (if not impossible) for me to tell you anything about what level of caching a JavaScript implementation has, because as a language, there isn’t any prescribed behavior.

    I think it would make sense to cache references to DOM elements for performance reasons, but I cannot speak to what each separate implementation does behind the scenes.

    As far as jQuery is concerned, there is no magical selector caching happening in the current version as far as I’m aware. Every time $(...) is called, it has to re-evaluate the selector. The selector could be a simple css-style selector, e.x. $('#foo .bar baz'), or it could be an element fragment, e.x. $('<div>foo bar baz</div>').

    If the jQuery function is passed a selector, it would have to re-check the DOM because new elements may have been added that could match the selector, or existing elements may have changed to no longer match the selector.

    As far as caching jQuery objects goes, it is common to save a reference to a jQuery object, and in some circumstances is more performant. The only way to know for sure which method is better is to run the script with a performance analyzer and see where the bottlenecks are. There’s no reason to just assume that writing your code in one particular way will improve the performance of your code.

    If you’re just trying to perform a series of operations on the same selection, be sure to take advantage of jQuery’s method chaining:

    $('.selector').addClass('foo').removeClass('bar').click(function () {...});
    

    jQuery methods typically return the original selection as a return value, which allows for a very elegant simplification of code.

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

Sidebar

Related Questions

Pretty basic question, I know... I just don't know how it's done. I want
I have a pretty basic question regarding the openxml sdk. I want to process
I know this a pretty basic question, and already found another ones like mine,
I know this is a pretty basic question, but I don't know how to
I'm pretty sure that this is a basic question but I really don't know
Pretty basic question ... I can not find the compatibility table (was looking at
It's a pretty basic question on matplotlib, but I cannot figure out how to
this is a pretty basic question but I can't seem to get it right.
This is a pretty basic question but I can't find a good answer for
This is probably a pretty basic question, but just something that I wanted to

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.