Skip to content

Instantly share code, notes, and snippets.

@xingrz
Created January 16, 2014 03:30
Show Gist options
  • Save xingrz/8449340 to your computer and use it in GitHub Desktop.
Save xingrz/8449340 to your computer and use it in GitHub Desktop.
function StringBuilder () {
this._strings = []
}
StringBuilder.prototype.append = function (str) {
this._strings.push(str)
}
StringBuilder.prototype.toString = function () {
return this._strings.join('')
}
console.time('plus')
var str = ''
for (var i = 0; i < 1000000; i++) {
str += 'text'
}
console.timeEnd('plus')
console.time('builder')
var builder = new StringBuilder()
for (var i = 0; i < 1000000; i++) {
builder.append('text')
}
console.timeEnd('builder')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment