@rmurphey-foo-foo-foo

JavaScript performance comparison

Revision 31 of this test case created by bga_

Info

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 code

<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>

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
for loop
var loopy = [];
for (var i = 0, j = myArray.length; i < j; i++) {
 loopy.push([myArray[i], myArray[i], myArray[i]].join(' '));
}
pending…
ES5 - forEach
var es5 = [];

function triple(item) {
 es5.push([item, item, item].join(' '));
}
myArray.forEach(triple);
pending…
ES5 - map
function triple(item) {
 return [item, item, item].join(' ');
}
var es5 = myArray.map(triple);
pending…
jQuery.each
var results = [];
$.each(myArray, function(index, item) {
 results.push([item, item, item].join(' '));
});
pending…
jQuery.map
var results = $.map(myArray, function(item) {
 return [item, item, item].join(' ');
});
pending…
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"));
}
pending…
regex loop/replace
var results = myArray.toString().replace(/(\d)/g, "$1 $1 $1").split(',');
pending…
regex loop/concat
var results = myArray.toString().replace(/\d/g, function(item) {
 return [item, item, item].join(' ');
}).split(',');
pending…
while loop with cached item
var loopy = [],
    i = 0,
    item;
while (item = myArray[i++]) {
 loopy.push(item + ' ' + item + ' ' + item);
}
// now that's fast
pending…
for loop, string replace
var results = [];
for (var i = 0, j = myArray.length; i < j; i++) {
 results[i] = "% % %".replace(/%/g, myArray[i]);
}
pending…
WR slim loop
for (var
loopy = [], i = myArray.length, str; i--;
loopy[i] = (str = myArray[i]) + " " + str + " " + str);
// now that's even faster ;-)
pending…
WR slick map
var map = myArray.map(cubeString);
// don't create a function for each
// test execution!!!
pending…
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
pending…
back loop, concat cache
for (var
loopy = [], i = myArray.length, str, str2; i--;
loopy[i] = (str2 = (str = myArray[i]) + " ") + str2 + str);
pending…
back loop String#concat
for (var
loopy = [], i = myArray.length, str; i--;
loopy[i] = (str = myArray[i]).concat(" ", str, ' ', str));
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:

0 comments

Add a comment