Comparing Pretty Joins

JavaScript performance comparison

Test case created by Pamela Fox

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Slice Approach
function greet(people) {
        var greeting = "Hello ";
        if (people.length === 0) {
                return greeting;
        } else if (people.length == 1) {
                return greeting + people[0];
        } else {
                return greeting + people.slice(0,-1).join(", ") + " and " + people[people.length-1];
        }
}

greet(["Pamela"]);
greet(["Pamela","Fiona"]);
greet(["Pamela","Fiona","Laura"]);

var names = [];
for (var i = 0; i < 1000; i++) {
  names.push('Ugo Tiago Marcondes Leal Chaves');
}
greet(names);
pending…
For Loop Approach
function greet(people) {
        var greeting = "Hello ";
        if (people.length == 1) {
                return greeting + people[0];
        } else {
                for(var i = 0; i < people.length - 2; i++){
                        greeting = greeting + people[i] + ', ';
                }
                return greeting + people[people.length-2] + " and " + people[people.length-1];
        }
}

greet(["Pamela"]);
greet(["Pamela","Fiona"]);
greet(["Pamela","Fiona","Laura"]);

var names = [];
for (var i = 0; i < 1000; i++) {
  names.push('Ugo Tiago Marcondes Leal Chaves');
}
greet(names);
pending…

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

Compare results of other browsers

0 comments

Add a comment