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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:31:57+00:00 2026-05-27T17:31:57+00:00

I came across a situation where having single handler helps simplify the project. Does

  • 0

I came across a situation where having single handler helps simplify the project. Does this have any impact on performance? Especially when the mouse move event called, having too many conditions have any impact on performance?

var myHandler = function(e){
    if (e.type == 'mousemove'){

    } else if (e.type == 'mouseover'){

    } else if (e.type == 'mouseout'){

    } else if (e.type == 'mousedown'){

    } else if (e.type == 'mouseup'){

    }        
};

$('#Element').bind('mouseover mouseout mousedown mouseup mousemove', myHandler);
  • 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-27T17:31:58+00:00Added an answer on May 27, 2026 at 5:31 pm

    If you do need to handle all those events and you order the if-else statements by the frequency that the events are fired (as you already have) there is insignificant performance penalty, i.e. at most 4 short string comparisons. The following code tries to benchmark the performance of 10,000,000 string comparisons of fixed size:

    $(function (){
    Function.prototype.benchmark = function(name, times, args){
    
    var iteration;
    var start = new Date();
    for (iteration=0; iteration<times; iteration++) {
            var result = this.apply(this, args);
    }
    
    var end = new Date();
    
    alert(name + " : " + (end-start));
    }
     function test(args){
         return args[0] == "thisistest";
     }
    function overhead(args){
     }
    test.benchmark("string comparison", 10000000,["thisistesT"]);
    //run same without the string comparison
    overhead.benchmark("overhead", 10000000,["thisistesT"]);
    });
    

    Since the browser is not the only application on my PC the results vary between the executions, however, I very rarely got results of under 100ms in Chrome (remember that this is for 10,000,000 iterations).

    Anyway, while your performance will not suffer from binding multiple events to a single function I really doubt that this will simplify your project. Having many if-else statements is usually considered a bad practice and a design flaw.

    If you do this so that you can share state between the handlers by having them under the common scope of a function, you are better off having something like:

    $(function (){
      var elementSelector = "#Element";
      var i = 0; //shared
      var events = {
        mouseover : function(){alert("mouseOver " + elementSelector + i++);},
        mouseout : function(){alert("mouseOut " + elementSelector  + i++);}
        // define the rest of the event handlers...
      };
    
      var $target = $(elementSelector);
      for(var k in events){
         $target.bind(k, events[k]);
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across with this situation. I have one already developed Application in 2.0,
I haven't actually came across this situation yet but i will probably have a
I came across this kind of situation but could not resolve, any hint would
I just came across an interesting situation in JavaScript. I have a class with
I came across this recently, up until now I have been happily overriding the
I came across this class while reading a C# book and have some questions.
I have came across many situation where I needed to pass value to an
working with a team of more traditional developers we came across this situation: We
I am fairly new to SSIS. I came across a situation where i have
I came across this very odd situation, and i thought i would throw it

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.