Cached DOM Traversal VS New ID selection
JavaScript performance comparison
Info
See this question: http://stackoverflow.com/questions/10424975/is-it-faster-to-traverse-the-dom-from-a-cached-selector-than-to-find-an-idd-ele
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div class="myclass" id="myid">
<a href="javascript:">Click me!</a>
</div>
<script>
Benchmark.prototype.setup = function() {
var link = $('a');
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Traverse .closest() |
|
pending… |
Fresh ID |
|
pending… |
Traverse .parent() |
|
pending… |
Traverse .parents() |
|
pending… |
Fresh object from class |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Jason
- Revision 2: published by Jason
0 comments