@rmurphey-foo-foo-foo (v11)

Revision 11 of this benchmark created by Kyle Simpson on


Description

Testing performance of different implementations for @rmurpheys challenge for creating and array of tripled string values.

http://gist.github.com/576723

Who said JavaScript OCD?

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  // 5: given the following array, create an array that contains the contents of
  // each array item repeated three times, with a space between each item. so,
  // for example, if an array item is 'foo' then the new array should contain an
  // array item 'foo foo foo'. (you can assume the library of your choice is
  // available)
  var myArray = ['foo', 'bar', 'baz', 'bat', 'batty', 'buzzy', 'booz'];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for loop
var loopy = [];
for (var i = 0, j = myArray.length; i < j; i++) {
 loopy.push([myArray[i], myArray[i], myArray[i]].join(' '));
}
ready
ES5 - forEach
var es5 = [];

function triple(item) {
 es5.push([item, item, item].join(' '));
}
myArray.forEach(triple);
ready
ES5 - map
function triple(item) {
 return [item, item, item].join(' ');
}
var es5 = myArray.map(triple);
ready
jQuery.each
var results = [];
$.each(myArray, function(index, item) {
 results.push([item, item, item].join(' '));
});
ready
for loop, regexp concat
var results = [];
var re = /(.*)/;
for (var i = 0, j = myArray.length; i < j; i++) {
 results.push(myArray[i].replace(re, "$1 $1 $1"));
}
ready
reverse while (with concat)
var i = myArray.length,
    result = new Array(i);

while (i--) {
 var item = myArray[i];
 result[i] = item + " " + item + " " + item;
}
ready
reverse while (with join)
var i = myArray.length,
    result = new Array(i);

while (i--) {
 var item = myArray[i];
 result[i] = [item, item, item].join(" ");
}
ready
fast for
var l = myArray.length,
    result = new Array(l);
for (var i = 0; i < l; i++) {
 var item = myArray[i];
 result[i] = item + " " + item + " " + item;
}
ready
Manual
var result = ['foo foo foo', 'bar bar bar', 'baz baz baz', 'bat bat bat', 'batty batty batty', 'buzzy buzzy buzzy', 'booz booz booz'];
ready
ES5 - map (slowest of them all!)
function triple(item) {
 return Array(4).join(' ' + item).substr(1);
}
var es5 = myArray.map(triple);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Morgan Roderick on
  • Revision 2: published by Matt S on
  • Revision 4: published by Tim Caswell on
  • Revision 9: published by Alex Sexton on
  • Revision 10: published by Kyle Simpson on
  • Revision 11: published by Kyle Simpson on
  • Revision 13: published by Alex Sexton on
  • Revision 14: published by Alex Sexton on
  • Revision 17: published by Ricardo Tomasi on
  • Revision 18: published by António Afonso on
  • Revision 25: published by WebReflection on
  • Revision 27: published by Alan Smithee on
  • Revision 29: published by Eddie Monge on
  • Revision 30: published by WebReflection on
  • Revision 31: published by bga_ on
  • Revision 42: published on