String repeat (v6)

Revision 6 of this benchmark created on


Preparation HTML

<script>

// https://github.com/sstephenson/prototype/blob/master/src/prototype/lang/string.js#L539
// http://stackoverflow.com/questions/202605/repeat-string-javascript/2433358#2433358
function prototypejs( n ) {
    return n < 1 ? '' : new Array( n + 1 ).join( this.valueOf() )
}

// http://stackoverflow.com/q/202605/489553
// optimized: this.valueOf(), while loop
function pushjoin( n ) {
  var result = [], pattern = this.valueOf(), i = n
  while (i--) result.push(pattern)
  return result.join('')
}

// http://stackoverflow.com/questions/202605/repeat-string-javascript/202626#202626
// optimized: this.valueOf(), while loop
function xn( n ) {
  var result = '', pattern = this.valueOf(), i = n
  while ( i-- ) result += pattern
  return result
}

// http://stackoverflow.com/questions/202605/repeat-string-javascript/4152613#4152613
function grow( n ){
  var result = ''
  var pattern = this
  while ( n > 0 ) {
    if ( n & 1 ) result += pattern
    n >>= 1
    pattern += pattern
  }
  return result
}

// final: growing pattern + prototypejs check (n < 1)
function final( n ) {
  if ( n < 1 ) return ''
  var result = '', pattern = this.valueOf()
  while ( n > 0 ) {
    if ( n & 1 ) result += pattern
    n >>= 1, pattern += pattern
  }
  return result
}

</script>

Setup

var n = 0x1000

Test runner

Ready to run.

Testing in
TestOps/sec
prototypejs
// https://github.com/sstephenson/prototype/blob/master/src/prototype/lang/string.js#L539
// http://stackoverflow.com/questions/202605/repeat-string-javascript/2433358#2433358
prototypejs( n )
ready
pushjoin
// http://stackoverflow.com/q/202605/489553
// optimized: this.valueOf(), while loop
pushjoin( n )
ready
xn
// http://stackoverflow.com/questions/202605/repeat-string-javascript/202626#202626
// optimized: this.valueOf(), while loop
xn( n )
ready
grow
// http://stackoverflow.com/questions/202605/repeat-string-javascript/4152613#4152613
grow( n )
ready
final
// final: growing pattern + prototypejs check (n < 1)
final( n )
ready

Revisions

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