Scotch Pig Latin

JavaScript performance comparison

Test case created by worldclassdev

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  // IMPERATIVE APPROACH
  function pigLatinImperative(str) {
    // Convert string to lowercase
    str = str.toLowerCase();
    // Initialize array of vowels
    const vowels = ["a", "e", "i", "o", "u"];
    // Initialize vowel index to 0
    let vowelIndex = 0;
  
    if (vowels.includes(str[0])) {
      // If first letter is a vowel
      return str + "way";
    } else {
      // If the first letter isn't a vowel i.e is a consonant
      for (let char of str) {
        // Loop through until the first vowel is found
        if (vowels.includes(char)) {
          // Store the index at which the first vowel exists
          vowelIndex = str.indexOf(char);
          break;
        }
      }
      // Compose final string
      return str.slice(vowelIndex) + str.slice(0, vowelIndex) + "ay";
    }
  }
  
  
  // DECLARATIVE APPROACH
  function pigLatinDeclarative(str) {
      return str
      .replace(/^([aeiouy])(.*)/, '$1$2way')
      .replace(/^([^aeiouy]+)(.*)/, '$2$1ay');
  }

};
</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
Imperative Approach
pigLatinImperative('consonant')
pending…
Declarative Approach
pigLatinDeclarative('consonant')
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.

0 Comments