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

  • SEARCH
  • Home
  • 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 8530549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:16:25+00:00 2026-06-11T09:16:25+00:00

I am trying to optimize some basic jquery script that traverses and hides/shows some

  • 0

I am trying to optimize some basic jquery script that traverses and hides/shows some unordered lists.

Here’s the testcase in jsperf: http://jsperf.com/caching-vs-no-caching

I run the test in two browsers: Chrome and IE7/IE8 and surprisingly the cached case is slower – by a little bit but still.

The unoptimized version is:

(function( $ ) {
  $.fn.menuManipulation = function() {
    this.parents().show();
  };
})( jQuery );

$('.menu-vertical li.selected').menuManipulation();
$(".menu-vertical li.selected > ul.static").show();
$('li.static').has('ul').addClass("with-child");

and the cached one:

(function($) {  
    $.fn.menuManipulation = function() {
    this.parents().show();
    };
})(jQuery);

var menu = $('.menu-vertical');
menu.find('li.selected').menuManipulation();
menu.find('li.selected > ul.static').show();
menu.find('li.static').has('ul').addClass("with-child");

Could someone explain what am I doing wrong and why does the cached version seem to be slower?

  • 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-11T09:16:26+00:00Added an answer on June 11, 2026 at 9:16 am

    Short answer: Selectors are actually pretty fast, but find is slow as hell. Your cached version has introduced multiple find calls – that is what is slow.

    Slightly longer answer: You only really get benefir from caching the jQuery collection if you keep re-using it as-is. Take a look at this test case where the cached version clearly runs faster: http://jsperf.com/cachingjq

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

Sidebar

Related Questions

I needed some help in trying to optimize this code portion ... Basically here's
I've got some OpenGL drawing code that I'm trying to optimize. It's currently testing
I am using jQuery 1.7.1 I am trying to optimize some jQuery and need
After putting some code to work, I'm trying to optimize it. One thing that
I am trying to optimize some code, that seems simple but is giving me
I've been trying to optimize some extremely performance-critical code (a quick sort algorithm that's
I am trying to optimize my MySQL queries and I need some help. Here
I'm trying to optimize some code that takes some test data stored in CSV
I'm trying to optimize up some horrendously complicated SQL queries because it takes too
I am trying some way to optimize following sql statement: exe_sql DELETE FROM tblEvent_type

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.