anagram

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
solution 1
var isAnagram = function(test, original) {
  if(test.length != original.length) return false;

  // Simpler solution, don't know about perf though
  test = test.toLowerCase().split("").sort().join("");
  original = original.toLowerCase().split("").sort().join("");
 
  return test == original;
};

isAnagram("foefet", "toffee");
pending…
solution 2
var isAnagram = function(test, original) {
  if(test.length != original.length) return false;


     var length = original.length;
     var index;
 
     test = test.toLowerCase();
     original = original.toLowerCase();
 
     for(var i=0; i<length; i++) {
       index = test.indexOf(original.charAt(i));
 
       if(index == -1) {
         return false;
       } else {
         test = test.split("").filter(function(el, idx) {
           return idx != index;
         }).join("");
       }
     }
 
     return test.length == 0;
};

isAnagram("foefet", "toffee");
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