sort-nodelist

JavaScript performance comparison

Test case created by

Preparation code

<div>
	<ul>
		<li id="item-2"><a href="#">item 2</a></li>
		<li id="item-1"><a href="#">item 1</a></li>
		<li id="item-3"><a href="#">item 3</a></li>
	</ul>
</div>
<script>
var toArray = this.toArray = function(list) {
	var retval = [];
	for (var i=list.length; i--;) {
		retval.push(list[i]);
	}
	return retval;
};
var a = document.getElementsByTagName('li');
</script>
    

Preparation code output

<div> <ul> <li id="item-2"><a href="#">item 2</a></li> <li id="item-1"><a href="#">item 1</a></li> <li id="item-3"><a href="#">item 3</a></li> </ul> </div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Nodelist to Array via native
[].slice.call(a);
pending…
Nodelist to Array via custom
toArray(a);
pending…
Sort Nodelist (native transformation)
[].sort.call([].slice.call(a), function(a, b) {
	return a.id > b.id ? 1 : -1;
});
pending…
Sort Nodelist (custom transformation)
[].sort.call(toArray(a), function(a, b) {
	return a.id > b.id ? 1 : -1;
});
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.

0 Comments