CoffeeScript's for vs. Underscore _.each vs. jQuery $.each
JavaScript performance comparison
Info
Compare for loop performance.
Preparation code
<script src="https://raw.github.com/documentcloud/underscore/ba3e31b53ef5752b40cfb2d71f594536fefbe916/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
var _i, _results;
window.array = (function() {
_results = [];
for (_i = 1; _i <= 1000; _i++){ _results.push(_i); }
return _results;
}).apply(this);
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
CoffeeScript for loop |
|
pending… |
_.each |
|
pending… |
$.each |
|
pending… |
array.forEach |
|
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 Lance Pollard
- Revision 2: published
- Revision 3: published by Onigoetz
- Revision 6: published
- Revision 7: published by hokaccha
- Revision 8: published
- Revision 9: published
- Revision 10: published
- Revision 11: published
- Revision 12: published
- Revision 14: published
2 comments
Unserscore test fails on ie9 due to MIME type mismatch. Github sends the script as text/plain, which is being blocked by ie.
syndrael: That's the result of compiling CoffeeScript into JavaScript. You can see the CoffeeScript equivalent in the comment (it's just one line): string += item.toString() for item in array
Go to coffeescript.org, click "Try CoffeeScript" at the top, and paste that one line to see the result.
"string += item.toString() for item in array"