Skip to content

Instantly share code, notes, and snippets.

@ZhangSen1
Created September 2, 2014 05:26
Show Gist options
  • Save ZhangSen1/2b6efa459ddd0ae309a0 to your computer and use it in GitHub Desktop.
Save ZhangSen1/2b6efa459ddd0ae309a0 to your computer and use it in GitHub Desktop.
StringBuilder
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
</head>
<body>
<script>
function StringBuilder(str) {
this.tmp = new Array();
}
StringBuilder.prototype.Append = function (value) {
this.tmp.push(value);
return this;
}
StringBuilder.prototype.Clear = function () {
this.tmp.length = 1;
}
StringBuilder.prototype.toString = function () {
return this.tmp.join('');
}
var start = new Date();
var str = "";
for (var i = 0; i < 1000000; i++) {
str += "text";
};
var end = new Date();
console.log(end.getTime() - start.getTime());
var start1 = new Date();
var build = new StringBuilder();
for (var i = 0; i < 1000000; i++) {
build.Append("text");
};
var dfd = build.toString();
var end1 = new Date();
console.log(end1.getTime() - start1.getTime());
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment