Skip to content

Instantly share code, notes, and snippets.

@brentsowers1
Created June 10, 2012 23:07
Show Gist options
  • Save brentsowers1/2907631 to your computer and use it in GitHub Desktop.
Save brentsowers1/2907631 to your computer and use it in GitHub Desktop.
jQuery attachment types test case 1
// Some action that triggers generating 10,000 elements
function generate10KElements(attachEventDirectly) {
var generateFunction = function() {
var containerElement = $("#container");
containerElement.html("");
if (!attachEventDirectly) {
// This is the magic, all items within the container div that have a CSS class
// of sameClassItem get the callbackFunction handler.
containerElement.on('click', ".sameClassItem", callbackFunction);
}
for (var i=0; i < 10000; i++) {
var element = $('<div class="sameClassItem" id="element' + i + '"><a href="#">click me</a> &nbsp;</div>');
containerElement.append(element);
if (attachEventDirectly) {
// The traditional way to attach a handler with jQuery
element.click(callbackFunction);
}
}
};
timeAction(this, generateFunction);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment