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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:34:01+00:00 2026-05-30T15:34:01+00:00

Someone recently told me that this is bad: var el = $(#myID); $(el).addClass(a); $(el).addClass(b);

  • 0

Someone recently told me that this is bad:

var el = $("#myID");
$(el).addClass("a");
$(el).addClass("b");
$(el).addClass("c");

and that it should be cached like so:

var $el = $("#myID");
$el.addClass("a");
$el.addClass("b");
$el.addClass("c");

So, my question is, why doesn’t it get optimized automatically? In Java and other languages I think that the compiler is smart enough to do the caching itself.

i.e., this:

// myList is a List<String>
String str = myList.get(0);
String trimmed = str.trim();
String sub = str.substring(0, 5);
boolean abc = str.startsWith("abc");

is no more efficient than this:

String trimmed = myList.get(0).trim();
String sub = myList.get(0).substring(0, 5);
boolean abc = myList.get(0).startsWith("abc");

Can anyone who knows more about compilers give me some insight here? Is JavaScript just stupid like that? Or is it also true for Java/others?

  • 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-30T15:34:03+00:00Added an answer on May 30, 2026 at 3:34 pm

    $(el) is a relatively expensive function call. The compiler cannot know that it will always return the same (or at least, an equivalent) object for each call, so it cannot make the optimisation you suggest.

    This is not necessarily a function of the language. I’m sure there will be languages (perhaps where you can declare a function as idempotent) in which the compiler CAN reason that the return value and do this optimisation.

    I’m pretty sure the Java compiler/optimiser can do it for relatively simple cases.

    So, for those same simple cases, there’s no reason why a Javascript compiler shouldn’t do it given the right circumstances. But it’s worth considering that the compilation happens in the browser, so there’s probably a limit to the amount of CPU time you want to spend doing optimisations so as not to impact the user’s browsing experience.

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

Sidebar

Related Questions

Recently someone stated that they thought all Creates should be CreateOrUpdates. Instinctively i thought
I was recently told that I should use transactions in my application as they
Someone else recently helped me out with the start of this question but I'm
Recently someone on Stack Overflow told me that the code below does not leak,
I've recently been working with someone else's code and I realized that this individual
Someone recently told me that there was a licencing constraint that prevented Android from
Someone has recently demonstrated to me that we can print variables in Python like
Someone recently told me that Scala's traits aren't true traits, and that they were
Someone recently asked me this question and I thought I'd post it on Stack
Recently someone pointed out that my personal site was not working on ipad, because

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.