@rmurphey-foo-foo-foo (v31)

Revision 31 of this benchmark created by bga_ 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>
function cubeString(str) {
    return str + " " + str + " " + str;
}
</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'];
</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
jQuery.map
var results = $.map(myArray, function(item) {
 return [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
regex loop/replace
var results = myArray.toString().replace(/(\d)/g, "$1 $1 $1").split(',');
ready
regex loop/concat
var results = myArray.toString().replace(/\d/g, function(item) {
 return [item, item, item].join(' ');
}).split(',');
ready
while loop with cached item
var loopy = [],
    i = 0,
    item;
while (item = myArray[i++]) {
 loopy.push(item + ' ' + item + ' ' + item);
}
// now that's fast
ready
for loop, string replace
var results = [];
for (var i = 0, j = myArray.length; i < j; i++) {
 results[i] = "% % %".replace(/%/g, myArray[i]);
}
ready
WR slim loop
for (var
loopy = [], i = myArray.length, str; i--;
loopy[i] = (str = myArray[i]) + " " + str + " " + str);
// now that's even faster ;-)
ready
WR slick map
var map = myArray.map(cubeString);
// don't create a function for each
// test execution!!!
ready
WR slick loop
for (var
loopy = [], i = myArray.length, str;
str = myArray[--i];
loopy[i] = str + " " + str + " " + str);
// should be slower than slim one
// due non boolean/integer 
// expression in the middle
ready
back loop, concat cache
for (var
loopy = [], i = myArray.length, str, str2; i--;
loopy[i] = (str2 = (str = myArray[i]) + " ") + str2 + str);
ready
back loop String#concat
for (var
loopy = [], i = myArray.length, str; i--;
loopy[i] = (str = myArray[i]).concat(" ", str, ' ', str));
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