| |
|
|
By Uzi Refaeli [ 28/10/2005 ] Publishing Free Articles Zone articles is subject to our Publisher's Terms Of Service |
|
In this series of articles I will try to provide some guidelines, tips and tricks about JavaScript performance.The first article will handle string concatenation, methods and performance guidelines which will make your JavaScript code better, allow it to run faster and provide smoother user experience.
The problem
Most JavaScript developers use the easy String manipulation possibilities which JavaScript offers, that is combining string with the + operator.
var str = "Hello" + " World";
var buffer = new Array();
buffer[buffer.length] = "Hello";
buffer[buffer.length] = " World";
buffer.join("");
function test1(){
var cycles = 10000;
var tp1 = new Date().valueOf();
var buffer = "";
for (var i = 0; i < cycles; i++)
buffer += "0123456789";
var tp2 = new Date().valueOf();
alert(tp2 - tp1);
}
function test2(){
var tp1 = new Date().valueOf();
var buffer = new Array();
for (var i = 0; i < cycles; i++)
buffer[buffer.length] = "0123456789";
var str = buffer.join("");
var tp2 = new Date().valueOf();
alert(tp2 - tp1);
}
About the author:
Uzi Refaeli is the CTO of Comet Information Systems which specialize with technology entrepreneurship and web development.
Article Source: http://www.Free-Articles-Zone.com