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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:01:58+00:00 2026-05-20T05:01:58+00:00

I’m trying to understand best practices for unobtrusive Javascript. Among the ideas of unobtrusive

  • 0

I’m trying to understand best practices for unobtrusive Javascript. Among the ideas of unobtrusive Javascript is that Javascript should not be directly included in the html code of the page, such as onchange=runScript(). Instead, an observer should be created to execute code whenever the element in question changes.

My questions are what the best practices are surrounding observers. How much overhead is there to an event observer in Javascript?

I don’t know how observers are generally coded in browsers. Is it more efficient to create a single general observer that could handle a lot of elements but has to run code for each click, such as the following:

document.body.observe('click', function(event) {
  if (event.element().match('some_id')) {
    //do something
    event.stop()
  }
}

Or would it be more efficient to create a single observer for each relevant element that matches it very specifically, such as the following:

$('some_id').observe('click', function(event) {
  //do something
  event.stop()
}

Or are observers of such low overhead that there’s no reason to worry about it at all, and I should just do whatever is more convenient to code?

  • 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-20T05:01:59+00:00Added an answer on May 20, 2026 at 5:01 am

    Unqualified, “efficient” is too broad to say that a single technique is the most efficient. For example, there’s time efficiency and space efficiency, and there’s often a tradeoff between the two. Also, different browsers are free to implement events however they see fit, as long as they behave as the W3C events standard dictates, limiting what conclusions can be made about time and space costs.

    We can analyze costs and efficiencies in relation to event flow. In every compliant browser, an event first fires on the root element, then on each intervening child element until it reaches the target, then the event bubbles back up to the root, again firing on intervening elements. There will be a time cost in making each step down and up the document structure during event flow (figuring out which element to fire on, looking up the listeners &c.), so if you can stop an event as early as possible during flow (as in your first example) there is a potential time saving. Of course, special handling of each target may offset any gain and it may be incorrect to stop an event early.

    Creating a separate but equal listener function for each of a collection of elements may produce an unnecessary memory cost. Note this is not the same thing as subscribing the same function on multiple objects. For example:

    for (var i=0; i < elements.length; ++i) {
        elements[i].observe('click', function (evt) {...});
    }
    

    may require more memory than:

    elements.each('click', function (evt) {...});
    

    If the JS engine interns functions, the two will use the same amount of memory.

    One thing we can’t assume is how event listeners are looked up, given an event and element. However, there typically aren’t that many listeners for a given event and element, so the time or space efficiency of this shouldn’t be a concern.

    In the end, browsers seem to implement events well enough that performance differences don’t tend to be an issue. Simplicity and readability are thus more important than how you define and subscribe event listeners.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.