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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:41:04+00:00 2026-06-03T03:41:04+00:00

I often works with jquery and sometimes my code contains lot of repeated css

  • 0

I often works with jquery and sometimes my code contains lot of repeated css class names, like:

$('#div1').addClass('funny-class');
...
$('#div2').addClass('funny-class');
...
$('#div1').removeClass('funny-class');

etc… I was asking to myself if is it worth to clean the code using pseudo constants like:

var constants = {
    FUNNY: 'funny-class'
}
...
$('#div1').addClass(cnst.FUNNY);
$('#div2').addClass(cnst.FUNNY);
$('#div1').removeClass(cnst.FUNNY);

First: minimization. If “constants” is a local var (hidden into a scope) probably the minimizer could replace the occurrence of “constants.FUNNY” with something minimized like “a.B”.

Second: speed. The code with “constants” is faster than the first one?

What do you think?

  • 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-03T03:41:05+00:00Added an answer on June 3, 2026 at 3:41 am

    Putting it in variables can provide you with a certain amount of “central control” rather than performance.

    However, putting them deeply in an object will incur a performance penalty. Keep them near the surface as possible to avoid the overhead in scope resolution. (it’s minimal, but still an overhead)

    //this one is so deep:
    constants.foo.bar.baz.bam
    
    //this is just 1 level deep:
    constants.bam
    

    Also, I’d worry more about the jQuery calls you are making

    //two calls to #div1!
    $('#div1').addClass(cnst.FUNNY);       //$() for div1
    $('#div2').addClass(cnst.FUNNY);
    $('#div1').removeClass(cnst.FUNNY);    //another $() for div1
    
    //do this instead
    var div1 = $('#div1')             //reference div1 once in a local variable
    
    div1.addClass(cnst.FUNNY);        //use the reference to the object
    $('#div2').addClass(cnst.FUNNY);
    div1.removeClass(cnst.FUNNY);     //use the same reference to the object
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often see examples of using the keyword this in jquery. Sometimes I see
If youa are serializing a form using something like jQuery, it will often map
jQuery plugins often have dependencies on external files: jQuery library, style sheets (CSS), images,
I often use this jQuery validation plugin to validate forms and it works great.
Git works fine on my dev box, but ever so often, I'll go to
I often like to create backups when testing the software I work on, and
Often, when working with jQuery, the need arises to include multiple plugins. This can
JQuery events are annoying me. The thing is that I very often use javascript
More a question about CSS, than about jQuery. I display top 7 players and
jQuery selectors are wonderful, but I sometimes I find myself typing them over and

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.