Max Recurring Character

JavaScript performance comparison

Test case created by worldclassdev

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let sampleText = 'ForloopingthroughthecharMapobject,weinitializetwovariablesatthebeginning.'
  
  function maxRecurringCharIteration(text) {
      let charMap = {}
      let maxCharValue = 0
      let maxChar = ''
      for (let char of text) {
          if (charMap.hasOwnProperty(char)) {
              charMap[char]++
          } else {
              charMap[char] = 1
          }
      }
      for (let char in charMap) {
          if (charMap[char] > maxCharValue) {
              maxCharValue = charMap[char]
              maxChar = char
          }
      }
      return maxChar
  }
  
  function maxRecurringCharArrays(text) {
      let charMap = {}
      let charArray =[]
      let vaulesArray = []
      let maxCharValue = 0
  
      for (let char of text) {
          if (charMap.hasOwnProperty(char)) {
              charMap[char]++
          } else {
              charMap[char] = 1
          }
      }
      charArray = Object.keys(charMap)
      vaulesArray = Object.values(charMap)
      maxCharValue = Math.max(...vaulesArray)
      
      return charArray[vaulesArray.indexOf(maxCharValue)]
  }

};
</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
For...in Iteration
maxRecurringCharIteration(sampleText)
pending…
Forming Arrays from a character map
maxRecurringCharArrays(sampleText)
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