Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Optimization

A hidden bonus of using "live" events is the optimization opportunity. Let me tell you about the "suitable" cases.

Case one, the obvious one — imagine a table with a thousand rows and a dozen columns, and now try to calculate how much memory click event handlers will eat up for each cell:

$("table td").on("click", function() { 
  /* ... */
});

I'm at a loss too. But I know exactly how to rewrite it to save memory and time:

$("table").on("click", "td", function() { 
  /* ... */
});

Case two, a contrived one — you need to log user actions on the page, meaning you need to track clicks on an innumerable number of objects:

$("body").on("click", "*", function() {
    console.info("Click on "+this.tagName);
});

To see this "contrived" example in action, open the sandbox, run the code above, and with the developer console open, click on some elements:

Try to explain what's happening.

Last updated