CoffeeScript's for vs. Underscore _.each vs. jQuery $.each

JavaScript performance comparison

Revision 2 of this test case created


Compare for loop performance.

Preparation code

<script src=""></script>
<script src=""></script>
Benchmark.prototype.setup = function() {
    var _i, _results;
    window.array = (function() {
      _results = [];
      for (_i = 1; _i <= 1000; _i++){ _results.push(_i); }
      return _results;

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
CoffeeScript for loop
// string += item.toString() for item in array

var string = "";
var i = 0;
var length = array.length;

for (i; i < length; i++) {
  item = array[i];
  string += item.toString();
var string = "";

_.each(array, function(item) {
  return string += item.toString();
var string = "";

$.each(array, function() {
  return string += this.toString();
var string = "";

array.forEach(function(item) {
  return string += item.toString();

Compare results of other browsers


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:


luke commented :

Unserscore test fails on ie9 due to MIME type mismatch. Github sends the script as text/plain, which is being blocked by ie.

Ricket commented :

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, click "Try CoffeeScript" at the top, and paste that one line to see the result.

"string += item.toString() for item in array"

Comment form temporarily disabled.

Add a comment