$.each - object supporting length key
JavaScript performance comparison
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div>asdasdasdads</div>
<div>asdasdasdads</div>
<div>asdasdasdads</div>
<div>asdasdasdads</div>
<script>
var divs = jQuery('div');
jQuery.newEach = function(object, callback, args) {
var name, i = 0,
length = object.length,
// The line below was the only lined I changed
isObj = !(object instanceof jQuery || length !== undefined && typeof length === "number" && ((length > 0 && object[0] && object[length - 1]) || jQuery.isArray(object))) || jQuery.isFunction(object);
if (args) {
if (isObj) {
for (name in object) {
if (callback.apply(object[name], args) === false) {
break;
}
}
} else {
for (; i < length;) {
if (callback.apply(object[i++], args) === false) {
break;
}
}
}
// A special, fast, case for the most common use of each
} else {
if (isObj) {
for (name in object) {
if (callback.call(object[name], name, object[name]) === false) {
break;
}
}
} else {
for (; i < length;) {
if (callback.call(object[i], i, object[i++]) === false) {
break;
}
}
}
}
return object;
}
// Gildas modification
jQuery.newEach2 = function(object, callback, args) {
var name, i = 0,
length = object.length,
// line added
o,
// The line below was the only lined Jordan changed
isObj = !(object instanceof jQuery || length !== undefined && typeof length === "number" && ((length > 0 && object[0] && object[length - 1]) || jQuery.isArray(object)));
if (args) {
if (isObj) {
for (name in object) {
if (callback.apply(object[name], args) === false) {
break;
}
}
} else {
for (; i < length;) {
if (callback.apply(object[i++], args) === false) {
break;
}
}
}
// A special, fast, case for the most common use of each
} else {
if (isObj) {
for (name in object) {
// line added
o = object[name];
// line changed
if (callback.call(o, name, o) === false) {
break;
}
}
} else {
for (; i < length; i++) {
// line added
o = object[i];
// line changed
if (callback.call(o, i, o) === false) {
break;
}
}
}
}
return object;
}
</script>
Preparation code output
asdasdasdads
asdasdasdads
asdasdasdads
asdasdasdads
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Old jQuery.each (raw obj) |
|
pending… |
New jQuery.each (raw obj) |
|
pending… |
Old jQuery.each (collection) |
|
pending… |
New jQuery.each (collection) |
|
pending… |
Old jQuery.each (normal array) |
|
pending… |
New jQuery.each (normal array) |
|
pending… |
Old jQuery.each (function) |
|
pending… |
New jQuery.each (function) |
|
pending… |
jQuery.newEach2(raw obj) |
|
pending… |
jQuery.newEach2(collection) |
|
pending… |
jQuery.newEach2 (normal array) |
|
pending… |
jQuery.newEach2 (function) |
|
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 Jordan Boesch
- Revision 2: published by Gildas
- Revision 3: published by Gildas
- Revision 4: published by Jordan Boesch
- Revision 5: published by Gildas
- Revision 6: published by Jordan Boesch
0 comments