@rmurphey-foo-foo-foo

JavaScript performance comparison

Test case created by Morgan Roderick

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

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