anagrams

JavaScript performance comparison

Test case created

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
filter dupes
var allAnagrams = function(string) {
        var prefix = "";
        var rest = string;
        var results = prefixChoices(prefix, rest);
        while (results[0].rest) {
                var stepResult = [];
                for (var i = 0; i < results.length; i++) {
                        stepResult = stepResult.concat(prefixChoices(results[i].prefix, results[i].rest));
                }
                results = stepResult;
        }
        return results.map(function(x) { return x.prefix});
};

var prefixChoices = function(prefix, rest) {
        var results = [];
        var beenSeen = {};
        for (var i = 0; i < rest.length; i++) {
                result = {
                        prefix: prefix + rest[i],
                        rest: rest.slice(0,i) + rest.slice(i+1, rest.length)
                }
                if (!beenSeen[result.prefix]) {
                        results.push(result);
                        beenSeen[result.prefix] = true;
                }
        }
        return results;
}

allAnagrams('cheeseen')
pending…
no filter
var allAnagrams = function(string) {
        var prefix = "";
        var rest = string;
        var results = prefixChoices(prefix, rest);
        while (results[0].rest) {
                var stepResult = [];
                for (var i = 0; i < results.length; i++) {
                        stepResult = stepResult.concat(prefixChoices(results[i].prefix, results[i].rest));
                }
                results = stepResult;
        }
        return results.map(function(x) { return x.prefix});
};

var prefixChoices = function(prefix, rest) {
        var results = [];
        for (var i = 0; i < rest.length; i++) {
                result = {
                        prefix: prefix + rest[i],
                        rest: rest.slice(0,i) + rest.slice(i+1, rest.length)
                }
                        results.push(result);
        }
        return results;
}
allAnagrams('cheeseen')
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