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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:59:44+00:00 2026-06-15T20:59:44+00:00

I was recently writing a blog post about checking if jquery elements exist before

  • 0

I was recently writing a blog post about checking if jquery elements exist before binding event handlers, I did a quick jsfiddle which you can see here

The thing I dont understand, is that the results show (using chrome to measure in microseconds) that test 2 is a lot faster then test 1.

You’ll see from the jsfiddle that test 2 checks the existent of the matching before binding a click event

TEST 1 is:

console.time('time_1');
    $('.yep').click(function() {
      alert('clicked');
    });
    console.timeEnd('time_1'); 

test 1 just tried to bind the event

TEST 2:

console.time('time_2');
if ($('.yep').length) {
  $('.yep').click(function() {
    alert('clicked');
  });
}
console.timeEnd('time_2');

test 2 check the element exists before binding.

I am running the two bits of code on some, 87 I think ‘section’ elemenets, one of which has a class of ‘yep’

I cant really see why the second test is faster, as its doing more work.

results:

time_1: 0.856ms 
time_2: 0.146ms 

Can anyone shed some light and help out a confused developer.

thanks

n.b please dont reply with alternative ways to bind click events in jquery, the .click is just used as a simple test

  • 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-15T20:59:45+00:00Added an answer on June 15, 2026 at 8:59 pm

    The primary thing going on here is that the first time you query a selector, the engine has to do more work than subsequent queries, which can sometimes be cached. If you reverse the first two tests, you’ll find that whichever one runs first tends to be the slower one.

    Despite that, and mostly as a side note, in test 2 you’re querying the DOM twice, first to check the length, and then to hook up the handler. If the query is cached it doesn’t matter much, but still, just do it once:

    console.time('time_x');
    var yep = $('.yep');
    if (yep.length) {
      yep.click(function() {
        alert('clicked');
      });
    }
    console.timeEnd('time_x');
    

    Note, though, that calling click on a jQuery set with no elements in it is a harmless no-op (not an error or anything), so there’s no need for the length check unless you’re also doing something else you haven’t shown.

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

Sidebar

Related Questions

I just recently found myself writing this line of code, which i did not
I am recently thinking about writing self-modifying programs , I think it may be
I recently asked a question about writing to multiple tables: PHP/MySQL insert into multiple
I have started writing articles for my blog recently and then realized that google
Recently I've decided to get some knowledge about writing custom tags. And there is
I recently started writing code that uses newer implementations of OpenGL. I did however
I recently stumbled across this entry in the google testing blog about guidelines for
I've read a lot recently about how writing multi-threaded apps is a huge pain
recently I was thinking about writing some midi related stuff. I've planned to use
I've been writing a lot recently about Parallel computing and programming and I do

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.