Scotch Anagrams Test

JavaScript performance comparison

Test case created by worldclassdev

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function isAnagramDirect(stringA, stringB) {
  
      const sanitizeString = function (str) {
          return str.toLowerCase().replace(/[^a-z\d]/g, '').split('').sort().join('');
      }
  
      return sanitizeString(stringA) == sanitizeString(stringB)
  
  }
  
  
  function isAnagramCharMap(stringA, stringB) {
  
      function createCharMap(text) {
          let charMap = {}
          for (let char of text) {
              if (charMap.hasOwnProperty(char)) {
                  charMap[char]++
              } else {
                  charMap[char] = 1
              }
          }
          return charMap
      }
  
      if (stringA.length === stringB.length) {
  
          let stringAMap = createCharMap(stringA)
          let stringBMap = createCharMap(stringB)
  
          for (let char in stringAMap) {
              if (stringAMap[char] !== stringBMap[char]) {
                  return false
              }
          }
  
          return true
      } else {
          return false
      }
  }

};
</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
Direct Comparison
isAnagramDirect('Scotchy is Scotch!', 'Scotch is Scotchy!')
pending…
Character Map Comparison
isAnagramCharMap('Scotchy is Scotch!', 'Scotch is Scotchy!')
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