Since JavaScript creates a new string instance for every time you concatenate strings, to simply keep adding strings together for HTML markup is still incredibly slow in JavaScript.
If you really want to crank up the speed, here is a MUCH faster way:
(1) Declare a new array to store the strings (e.g. var aHTML = new Array();)
(2) Push every new string of HTML markup to the end of the array using the Array object's push method (e.g. aHTML.push(' '); aHTML.push(' '); aHTML.push(' My Page '); aHTML.push('');)
(3) When it is time to combine the strings for writing, concatenate ALL of the strings at once with the Array object's join method, including a null string as the argument, so that the strings will be combined with no extra markup in between them (e.g. document.write(aHTML.join(''));)
Try running a long string concatenation next to this method and see which one wins.
Discussion on:
View:
Show:
I've also heard that it's better to store the value of the .length property in a local variable when using it for loops.
example:
len = arr.length;
for( i = 0; i len; i++ ) { ... }
since calling 'arr.length' requires a lookup each time through an iteration.
How significant is this?
example:
len = arr.length;
for( i = 0; i len; i++ ) { ... }
since calling 'arr.length' requires a lookup each time through an iteration.
How significant is this?
Here are some more performance tips that I have collected from various sources on the Internet over the years. They're very helpful.
>> Object Caching - Cache Object references (especially DOM Objects). Avoid Object chains. For example -
for(var i=0; i document.images.length; i++)
document.images.src = ?someImg.gif?;
In the above snippet, "document.images" collection is being accessed twice per loop (once for i document.images.length and the other for assigning something to the src property - document.images.src = ?someImg.gif?;.
This is inefficient as the browser has to access the document object, then the images collection, then the actual array item. Instead,
var theimages = document.images // Set a reference to document.images
for (i=0; i Precalculate Length -
for(var i = 0; i collection.length; i++){ }
can be more efficiently written as:
for(var i = 0, len = collection.length; i Use [] Instead of .item()
E-mail me for a more comprehensive DOC on performance considerations and useful conventions to follow while coding in Javascript.
>> Object Caching - Cache Object references (especially DOM Objects). Avoid Object chains. For example -
for(var i=0; i document.images.length; i++)
document.images.src = ?someImg.gif?;
In the above snippet, "document.images" collection is being accessed twice per loop (once for i document.images.length and the other for assigning something to the src property - document.images.src = ?someImg.gif?;.
This is inefficient as the browser has to access the document object, then the images collection, then the actual array item. Instead,
var theimages = document.images // Set a reference to document.images
for (i=0; i Precalculate Length -
for(var i = 0; i collection.length; i++){ }
can be more efficiently written as:
for(var i = 0, len = collection.length; i Use [] Instead of .item()
E-mail me for a more comprehensive DOC on performance considerations and useful conventions to follow while coding in Javascript.
EtchEmKay, you said:
E-mail me for a more comprehensive DOC on performance considerations and useful conventions to follow while coding in Javascript.
I would be interested in seeing this doc, however you haven't listed your email address. Perhaps you could post the doc or email me at summersaultATcanada.com?
thanks
E-mail me for a more comprehensive DOC on performance considerations and useful conventions to follow while coding in Javascript.
I would be interested in seeing this doc, however you haven't listed your email address. Perhaps you could post the doc or email me at summersaultATcanada.com?
thanks
By any standards, assigning the .length value to a variable is better.
I holds true for almost any language even in vbscript it is advisable to assign the value of UBOUND(arrayVar)to a var.
I holds true for almost any language even in vbscript it is advisable to assign the value of UBOUND(arrayVar)to a var.
- Keyboard Shortcuts:
- Prev
- Next
- Toggle









































