loop-vs-iterator-reduce-corrected

JavaScript performance comparison

Test case created by babakness

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  
  function reduce(
      initialValue,
      reducer,
      data,
    ) {
      let acc = initialValue
      let count = 0
      for ( const item of data ) {
        acc = reducer( acc, item, count++ )
      }
      
      return acc
  }
  
  function longestWordLengthLoop( str ) {
    const words = str.split( /\W+/g )
    let longestLength = 0
    for ( const word of words ) {
      longestLength = Math.max( longestLength, word.length )
    }
    return longestLength
  }
  
  const longestWordLengthReducer = ( longestLength, word ) => {
    return Math.max( longestLength, word.length )
  }
  
  
  const longestWordLengthReduce = str => reduce( 
      0, 
      longestWordLengthReducer, 
      str.split( /\W+/g )
  )
  

};
</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
loop
longestWordLengthLoop('she sells sea shells at the sea shore')
pending…
reduce
longestWordLengthReduce('she sells sea shells at the sea shore')
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