[Codewars] Duplicate Encoder Kata

JavaScript performance comparison

Test case created by Dani Guardiola

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  // Adapted from http://stackoverflow.com/a/1349426
  function genString() {
    const arr = [];
    const possible = 'ABCKabc012';
  
    for (let i = 0; i < 5; i++) {
      const char = possible.charAt(Math.floor(Math.random() * possible.length));
      arr.push();
    }
  
    return arr.join('');
  }
  
  const string = genString();

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Long optimized solution
function duplicateEncode(word) {
  // Transform to lowercase
  const lowerCase = word.toLowerCase();
  
  // Human-readable values for char status
  const NOT_DUPLICATE = 1;
  const DUPLICATE = 2;
  
  // Check duplicated chars
  const chars = {};
  for (let i = lowerCase.length - 1; i >= 0; i--) {
  // Reversed (backwards) loop for performance http://jsperf.com/caching-array-length/40
    const char = lowerCase[i];
    chars[char] = chars[char] ? DUPLICATE : NOT_DUPLICATE;
  }  
  
  // Create output strings
  const outputArray = [];
  for (let i = 0, len = lowerCase.length; i < len; i++) {
  // Loop (ordered) with cached array length for performance http://jsperf.com/caching-array-length/40
    const inChar = lowerCase[i];
    const outChar = chars[inChar] === DUPLICATE ? ')' : '(';
    outputArray.push(outChar);
  }
  
  // Return concatenated output
  return outputArray.join('');
}

duplicateEncode(string);
pending…
Short solution #1
// https://www.codewars.com/kata/reviews/54dd632b04bd8bc1a2000014/groups/54f593dbabe2403022000195
function duplicateEncode(word){
  return word
    .toLowerCase()
    .split('')
    .map( function (a, i, w) {
      return w.indexOf(a) == w.lastIndexOf(a) ? '(' : ')'
    })
    .join('');
}

duplicateEncode(string);
pending…
Short solution #9
const duplicateEncode = word => {
  word = word.toLowerCase();
  return word.replace(/./g, w => word.indexOf(w) == word.lastIndexOf(w)? '(': ')');
}

duplicateEncode(string);
pending…
Short solution #4
function duplicateEncode(string){
   var occurances = string.toLowerCase().split('').reduce(function(occ, cha){
      occ[cha] = (occ[cha] || 0) + 1;
      return occ;
   }, {});
   return string.toLowerCase().replace(/[\S\s]/g, function(cha){
      return occurances[cha] > 1 ? ')' : '(';
   });
}

duplicateEncode(string);
pending…
Short solution #5
function duplicateEncode(word) {
  word = word.toLowerCase();
  return word.replace(/./g, m => word.indexOf(m) == word.lastIndexOf(m) ? '(' : ')');
}

duplicateEncode(string);
pending…
Short solution #8
function countCharacters(chars) {
  return chars
    .reduce( function(memo, char){
      memo[char] = memo[char] ? memo[char] + 1 : 1;
      return memo;
    }, {});
}

function duplicateEncode(word){
  const chars = word.split('').map(ch => ch.toLowerCase());
  const charsCount = countCharacters(chars);
  return chars
    .map( ch => charsCount[ch] > 1 ? ')' : '(' )
    .join('');
}

duplicateEncode(string);
pending…
Short solution #6
function duplicateEncode(word){
  function count(word, char) {
    var result = 0;
    for (var index = 0, len = word.length; index < len; index++) {
      if (word[index] == char) result++;
    }
      
    return result;
  }
    
  var result = [];
  var lowerCasedWord = word.toLowerCase();
  for (var index = 0, len = lowerCasedWord.length; index < len; index++) {
    if (count(lowerCasedWord, lowerCasedWord[index]) > 1)
      result.push(")");
    else
      result.push("(");
  }  
  
  return result.join('');
}

duplicateEncode(string);
pending…
Short solution #3
function duplicateEncode(word){
   
    var unique='';
    word = word.toLowerCase();
    for(var i=0; i<word.length; i++){
        if(word.lastIndexOf(word[i]) == word.indexOf(word[i])){
            unique += '(';
        }
        else{
            unique += ')';
        }
    }
    return unique;

}

duplicateEncode(string);
pending…
Short solution #7
function duplicateEncode(word){
    return word.toLowerCase().replace(/./g, function(match) { return word.toLowerCase().split(match).length > 2 ? ')' : '(' ;});
}

duplicateEncode(string);
pending…
Short solution #2
function duplicateEncode(word) {
  var letters = word.toLowerCase().split('')
  return letters.map(function(c, i) {
    return letters.some(function(x, j) { return x === c && i !== j }) ? ')' : '('
  }).join('')
}

duplicateEncode(string);
pending…

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

0 Comments