Skip to content

Commit

Permalink
Improve performance on large DOMs
Browse files Browse the repository at this point in the history
When the container is the document, there's no need to be fancy when
loading Elemental and search for attributes of [data-behavior]. We know
we can simple search the entire document.

This is especially helpful on IE8, where the .filter() for attribute
selectors is super slow.
  • Loading branch information
Ian Zabel committed May 13, 2013
1 parent 4e4a18c commit 50f795a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
33 changes: 28 additions & 5 deletions spec/elemental_spec.js
Expand Up @@ -38,11 +38,34 @@ describe("Elemental", function(){
expect(bar).toHaveBeenCalled();
});

it("should load a behavior nested deeply beneath the container element", function() {
baz = jasmine.createSpy('baz');
var container = "<div><div><div><div data-behavior='baz'> </div></div></div></div>";
Elemental.load(container);
expect(baz).toHaveBeenCalled();
describe("when the container is some specific DOM", function() {
it("should load a behavior nested deeply beneath the container element", function() {
baz = jasmine.createSpy('baz');
var container = "<div><div><div><div data-behavior='baz'> </div></div></div></div>";
Elemental.load(container);
expect(baz).toHaveBeenCalled();
});

it("should not load a behavior that is outside of the container element", function() {
baz = jasmine.createSpy('baz');
qux = jasmine.createSpy('qux');
var container = "<div data-behavior='baz'> </div>";
setFixtures("<div data-behavior='qux'> </div>");

Elemental.load(container);

expect(baz).toHaveBeenCalled();
expect(qux).not.toHaveBeenCalled();
});
});

describe("when the container is the document", function() {
it("should load a behavior nested deeply beneath the container element", function() {
buzz = jasmine.createSpy('buzz');
setFixtures("<div><div><div><div data-behavior='buzz'> </div></div></div></div>");
Elemental.load(document);
expect(buzz).toHaveBeenCalled();
});
});

it("should not load a behaviour that does not exist", function(){
Expand Down
10 changes: 9 additions & 1 deletion src/elemental.js
Expand Up @@ -29,7 +29,15 @@
};

ns.load = function(container) {
$(container).find("*").andSelf().filter("[data-behavior]").each(function(index, element) {
var $selector;
if (container === document) {
$selector = $('[data-behavior]');
}
else {
$selector = $(container).find("*").andSelf().filter("[data-behavior]");
}

$selector.each(function(index, element) {
var $element = $(element);
var behaviors = $element.data('behavior');
behaviors.replace(/([^ ]+)/g, function(behavior) {
Expand Down

0 comments on commit 50f795a

Please sign in to comment.