JS Challenge 2

JavaScript performance comparison

Revision 24 of this test case created

Info

The second code challenge @ DM

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [
      ['lehlowww', 'hello'],
      ['dlrwxdonexfe', 'oneworld'],
      ['oenhodwrol', 'oneworld'],
      ['rkqodlw', 'world'],
      ['olaelh', 'hello'],
      ['helio', 'hell'],
      ['abcewnorde', 'wonder'],
      ['wioskrngatwo', 'workingtwo'],
      ['locaacedo', 'locatedhere'],
      ['edfbenowaginning', 'beginningnow'],
      ['CAPS TEST', 'caps test'],
    ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent et mauris magna, ac dapibus ipsum. Sed viverra feugiat vehicula. Pellentesque vel lacinia ipsum. Vestibulum vel diam vel velit mollis adipiscing. Suspendisse potenti. Duis elementum erat vitae neque blandit porttitor. Pellentesque pellentesque vulputate posuere. Pellentesque eget justo elit. Praesent elementum odio sit amet lectus dignissim tincidunt. In quam massa, varius nec convallis quis, luctus sed erat. Cras ut massa metus. Integer lacinia iaculis pretium.', 'luctus sed erat']
    ];
   
    var answers = [true,false,true,true,true,false,true,true,false,true, false, true];
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Michael K
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Michael K failed test '+i);
    throw 'failed test '+i;
  }
}

function StringScramble(clone, orig) {
  var match = 0
  var fail = 0;
 // var clone = "this is my test string";
  var cloneLow = clone.toLowerCase();
  var cloneArr = cloneLow.split("");
  //var orig = "i miss test";
  var origLow = orig.toLowerCase();
  var origArr = origLow.split("");
  for (var i = 0; i < clone.length; i++) {
    if (orig.indexOf(clone[i]) == -1) {
      fail++;
    } else {
      match++;
    }
  }
  if (match >= origArr.length) {
    return true;
  } else {
    return false;
  }
}
pending…
Max H
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Max H failed test '+i);
    throw 'failed test '+i;
  }
}

function StringScramble(str1, str2) {
  var arr = new Array(26);
  var index;
  var arr_length = arr.length;
  var str1_length = str1.length;
  var str2_length = str2.length;
  // Initialize the array to 0
  for (var i = 0; i < arr_length; i++) {
    arr[i] = 0;
  }

  str2 = str2.toLowerCase();
  // Put char counts into arr for str2
  for (var j = 0; j < str2_length; j++) {
    index = str2[j].charCodeAt(0) - 97;
    arr[index] = arr[index] + 1;
  }

  // Compare str1 to str2
  for (var k = 0; k < str1_length; k++) {
    index = str1[k].charCodeAt(0) - 97;
    if (arr[index] > 0) arr[index] = arr[index] - 1;
  }

  // If any of the elements in the arry is non 0,
  // there's a mismatch and str1 cannot be a permutation of str2
  for (var n = 0; n < arr_length; n++) {
    if (arr[n] != 0) return false;
  }
  return true;
}
pending…
Spencer W
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Spencer W failed test '+i);
    throw 'failed test '+i;
  }
}

function StringScramble(str1, str2) {
    var scrambled = str1.split("").sort();
    var expected = str2.split("").sort();
    var result = [];
    var ii = scrambled.length;
    var jj = expected.length;

    for (var i = 0; i < ii; i++) {
      for (var j = 0; j < jj; j++) {
        if (scrambled[i] === expected[j]) {
          result.push(scrambled[i]);
          break;
        }
      }

    }
    var scrambledString = result.toString();
    var expectedString = expected.toString();

    return (scrambledString === expectedString ? true : false);

}
pending…
Jenni
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Jenni failed test '+i);
    throw 'failed test '+i;
  }
}

function StringScramble(str1, str2) {
  var s1 = str1.split(""),
      s2 = str2.split(""),
      l1 = s1.length,
      l2 = s2.length,
      a1 = []; // duplicate array accounts for double letters
  for (var j = 0; j < l1; j++) {
    if (a1[s1[j]]) {
      a1[s1[j]]++;
    } else {
      a1[s1[j]] = 1;
    }
  }

  for (var i = 0; i < l2; i++) {
    if (a1[s2[i]] > 0) {
      a1[s2[i]]--;
    } else {
      return false;
    }
  }
  return true;
}
pending…
Frank
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Frank failed test '+i);
    throw 'failed test '+i;
  }
}

function StringScramble(str1, str2) {

  // Easy fail, if str1 isn't long enough
  if (str1.length < str2.length) {
    return false;
  }

  // Basic approach:
  // Sort the letters, then compare them
  // one by one, and bail out as soon as
  // we see a letter in s2 that s1 can't supply
  list1 = str1.split('').sort();
  list2 = str2.split('').sort();

  var c1 = list1.pop();
  var c2 = list2.pop();
  while (c1 && c2) {
    if (c1 == c2) {
      c1 = list1.pop();
      c2 = list2.pop();
    } else if (c1 < c2) {
      // str2 just asked for a char str1 can't provide
      return false;
    } else {
      // burn off excess str1 chars
      c1 = list1.pop();
    }
  }

  // a value leftover in c2 means that str1 dried up
  // before matching all of str2's needs.  Fail!
  return (!c2);
}

//trues
//StringScramble("cat", "act")
//StringScramble("cattle", "act")
//StringScramble("abbbba", "b")
//StringScramble("cattle", "")
//StringScramble("", "")
//falses
//StringScramble("cat", "actor")
//StringScramble("cattle", "lettuce")
//StringScramble("", "b")
pending…
Kyle
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Kyle failed test '+i);
    throw 'failed test '+i;
  }
}

function StringScramble(str1, str2) {

    var count = 0;
   
    var lowerstr1 = str1.toLowerCase();
    var lowerstr2 = str2.toLowerCase();
    var splitarr1 = lowerstr1.split("");
    var splitarr2 = lowerstr2.split("");

    var length1 = splitarr1.length;
    var length2 = splitarr2.length;

    for (var i = 0; i < length2; i++) {
        for (var j = 0; j < length1; j++) {
//          alert ("Comparing " + splitarr2[j] + " to " + splitarr1[i]);
            if (splitarr2[j] === splitarr1[i]) {
                 splitarr1[i] = "*";
                count++;          
                break;
            }

            if (count == length2) {
                    return "true";
            }            

        }



    }
    return "false"
   
}
pending…
Sam P
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Sam P failed test '+i);
    throw 'failed test '+i;
  }
}

function StringScramble(str1, str2) {
  if (str2.length > str1.length) return false;

  var arr1 = [];
  for (var i = 0, length = str1.length; i < length; i++) {
    arr1.push(str1.charCodeAt(i));
  }

  var arr2 = [];
  for (i = 0, length = str2.length; i < length; i++) {
    arr2.push(str2.charCodeAt(i));
  }

  for (var i = 0; i < arr2.length; i++) {
    if ((x = arr1.indexOf(arr2[i])) > -1) arr1.splice(x, 1);
    else return false;
  }

  return true;
}
pending…
Phil H
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Phil H failed test '+i);
    throw 'failed test '+i;
  }
}

function StringScramble(str1, str2) {
  var pool = str1.split('').sort();
  var goal = str2.split('').sort();
  for (var i = 0; i < pool.length; i++) {
    if (pool[i] < goal[0]) continue;
    if (pool[i] == goal[0]) goal.shift();
    if (goal.length == 0) return true;
  }
  return false;
}
pending…
Scott
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Scott failed test '+i);
    throw 'failed test '+i;
  }
}

function StringScramble(str1, str2) {
                var len = str2.length,
                        i = 0,
                        pos;
                str1 = str1.split('');
                for (; i < len; i += 1) {
                        pos = str1.indexOf(str2[i]);
                        if (pos === -1) return false;

                        str1[pos] = '.';

                }

                return true;
        }
pending…
Chin Y
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Chin failed test '+i);
    throw 'failed test '+i;
  }
}

function StringScramble(str1, str2) {
    for (i=0; i<str2.length; i++) {
        pos = str1.indexOf(str2[i]);
        if (pos === -1) return false;

        str1 = str1.substr(0, pos) + str1.substr(pos + 1);
    }
    return true;
}
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