JavaScript Performance
How to speed up your JavaScript code.
Reduce Activity in Loops
Loops are often used in programming.
Each statement in a loop, including the for statement, is executed for each iteration of the loop.
Statements or assignments that can be placed outside the loop will make the loop run faster.
Bad:
for (let i = 0; i < arr.length; i++) {
Better Code:
let l = arr.length;
for (let i = 0; i < l; i++) {
The bad code accesses the length property of an array each time the loop is iterated.
The better code accesses the length property outside the loop and makes the loop run faster.
Reduce DOM Access
Accessing the HTML DOM is very slow, compared to other JavaScript statements.
If you expect to access a DOM element several times, access it once, and use it as a local variable:
Example
const obj = document.getElementById("demo");
obj.innerHTML = "Hello";
Reduce DOM Size
Keep the number of elements in the HTML DOM small.
This will always improve page loading, and speed up rendering (page display), especially on smaller devices.
Every attempt to search the DOM (like getElementsByTagName) will benefit from a smaller DOM.
Avoid Unnecessary Variables
Don’t create new variables if you don’t plan to save values.
Often you can replace code like this:
let fullName = firstName + " " + lastName;
document.getElementById("demo").innerHTML = fullName;
With this:
document.getElementById("demo").innerHTML = firstName + " " + lastName;
Delay JavaScript Loading
Putting your scripts at the bottom of the page body lets the browser load the page first.
While a script is downloading, the browser will not start any other downloads. In addition all parsing and rendering activity might be blocked.
The HTTP specification defines that browsers should not download more than two components in parallel.
An alternative is to use defer="true"
in the script tag. The defer attribute specifies that the script should be executed after the page has finished parsing, but it only works for external scripts.
If possible, you can add your script to the page by code, after the page has loaded:
Example
<script> window.onload = function() { const element = document.createElement("script"); element.src = "myScript.js"; document.body.appendChild(element); }; </script>
Avoid Using with
Avoid using the with
keyword. It has a negative effect on speed. It also clutters up JavaScript scopes.
The with
keyword is not allowed in strict mode.
40 Responses
levaquin 250mg ca order levaquin 500mg
order dutasteride pill flomax pills oral ondansetron 8mg
aldactone 100mg tablet buy fluconazole 100mg online order fluconazole 200mg sale
buy ampicillin 500mg pill cheap ampicillin 250mg buy erythromycin 250mg without prescription
order fildena without prescription bimatoprost allergy spray order generic robaxin
sildenafil pills sildalis generic estradiol 1mg over the counter
cost lamictal 50mg buy lamictal 50mg pill tretinoin cheap
tadalis 10mg ca tadalafil 10mg over the counter voltaren generic
order isotretinoin 20mg without prescription oral amoxicillin 250mg zithromax pill
order indocin generic order generic cefixime 100mg order amoxicillin 500mg generic
tadalafil 5mg uk Viagra original pfizer order sildenafil price
generic arimidex anastrozole 1 mg canada order sildenafil online cheap
cialis gГ©nГ©rique acheter sildenafil viagra pas cher
prednisone 10mg without prescription best canadian pharmacy cost of viagra 100mg
tadalafil 5mg kaufen generika sildenafil 50mg kaufen ohne rezept viagra 100mg kaufen generika
cost isotretinoin 20mg purchase isotretinoin pill stromectol otc
order modafinil 200mg generic provigil 100mg sale acetazolamide 250 mg sale
order altace 10mg without prescription buy ramipril generic azelastine 10 ml without prescription
catapres 0.1mg pills clonidine brand buy tiotropium bromide 9mcg
buspar drug dilantin over the counter oxybutynin 2.5mg generic
order hytrin 5mg generic oral terazosin 5mg purchase sulfasalazine pills
fosamax 70mg pill fosamax sale buy pepcid 20mg for sale
benicar 20mg pills purchase depakote pills order generic diamox
buy tacrolimus pill tacrolimus 1mg drug buy urso 150mg generic
buy bupropion 150 mg generic buy strattera 25mg without prescription seroquel 100mg usa
buy molnunat 200mg pills oral cefdinir 300 mg prevacid 15mg pills
sertraline 50mg uk cost zoloft 50mg sildenafil 50mg cheap
order salbutamol pill sildenafil 50mg tablets buy viagra 100mg online cheap
order tadalafil 10mg without prescription order symmetrel 100 mg sale buy amantadine sale
order revia 50mg sale buy naltrexone 50mg without prescription purchase aripiprazole pills
avlosulfon 100mg cost dapsone 100mg ca aceon over the counter
buy generic medroxyprogesterone 5mg cheap praziquantel order periactin 4 mg
modafinil without prescription oral provigil 200mg stromectol for sale online
luvox 100mg pill nizoral 200mg uk glucotrol 10mg over the counter
isotretinoin 10mg cost deltasone 20mg pills order deltasone without prescription
buy piracetam 800 mg generic buy sildenafil 50mg for sale brand sildenafil 50mg
zithromax medication buy prednisolone generic gabapentin cheap
order cialis 20mg generic order sildenafil 50mg purchase viagra sale
order furosemide pill furosemide without prescription order plaquenil online cheap
tadalafil 20mg oral order generic betamethasone 20gm buy clomipramine 50mg generic