Very simple JavaScript tuning

I noticed that a piece of JavaScript code takes a long time to execute. So, i created a simple time stamp code to help me better understand how much time the code takes to execute:

var startTime = new Date();
// do some processing
var endTime = new Date();
var timeDiff = endTime.getTime() - startTime.getTime();

After some tweaking, I found out that if I create HTML on a temporary variable and do a single innerHTML call at the end is significantly faster than calling innerHTML on every little piece of HTML code that I want to add. This simple change improved my code from 1800 ms to 25 ms, what a big difference!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s