Created
February 3, 2012 23:38
-
-
Save zallarak/1733755 to your computer and use it in GitHub Desktop.
String concatenation in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function StringBuffer() { | |
| this.__strings__ = new Array; | |
| } | |
| StringBuffer.prototype.append = function(str) { | |
| this.__strings__.push(str); | |
| }; | |
| StringBuffer.prototype.toString = function() { | |
| return this.__strings__.join(" "); | |
| }; | |
| var d1 = new Date(); | |
| var str = ""; | |
| for (var i=0; i<1000000; i++) { | |
| str += "text"; | |
| } | |
| var d2 = new Date(); | |
| document.write("Concatenation with a plus: " + (d2.getTime() - d1.getTime()) + " milliseconds </br>") | |
| var oBuffer = new StringBuffer(); | |
| d1 = new Date(); | |
| for (var i=0; i<1000000; i++) { | |
| oBuffer.append("text"); | |
| } | |
| d2 = new Date(); | |
| document.write("Concatenation with a StringBuffer: " + (d2.getTime() - d1.getTime()) + " milliseconds") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment