5 Replies - 1253 Views - Last Post: 12 April 2012 - 07:30 AM

#1 MrSooty  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 85
  • Joined: 14-September 11

The effects of a large array on page load times and reliability

Posted 12 April 2012 - 03:55 AM

I've been given a job by our client to extract a crap-ton of data from their offline webpages (distribute on CD) so that they can easily edit the content without having to trawl through pages and pages of HTML. I've decided to take all of the data and store it into an associative array. The client is looking to support multi languages.

I have a folder for English definitions called en.js. This folder at the moment (I'm not done but I'm close to finishing) stands at about 5500 lines. Each line contains 1 definition (give ~200 for line breaks) for a placeholder name in the HTML. As an example, I would have

HTML
<span id="MainPageSubTitle"></span>
<script>putContent("MainPageSubTitle","MainPageSubTitle")</script>



JS - I do have a lot more code than this (error handling etc) but it doesnt have much to do with my question, so I didn't post it all, just for clarity.
var b = {};
fucntion addToArray(str1,str2){
      b[str1]=str2;
}
function $(str1,str2){
     addToArray(str1,str2);
}
function putContent(id,name){
    document.getElementById(id).innerHTML = b[name];
}



en.js (here is the definition I speak of)
   $("MainPageSubTitle","This is an example sub title that the client can edit from the en.js file");



Output / view source
    <span id="MainPageSubTitle">This is an example sub title that the client can edit from the en.js file</span>



I have only started using Javascript these last 6 months so my knowledge as to how the nitty gritty stuff works is fairly limited. I've also never done big projects before so any array bigger than say 10 elements long is huge in my eyes :)

So my question is this:

With 5500 lines of definitions similar to the one above (some longer, some shorter), what sort of impact does this have on slower machines and, generally speaking, how large is too large? The fact I have that many strings makes me worry a tad (needlessly or not, you tell me!)

Note: This has taken me weeks of copy/pasting to create, so I'm not too interested in changing the underlying structure, I'm purely interested in the theoretical side of this. Sorry if my question was super long winded!

EDIT: I think it's worth mentioning that each page might only use about 200 definitions. So there will be 200 or so calls to place the content on the page.

This post has been edited by MrSooty: 12 April 2012 - 04:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: The effects of a large array on page load times and reliability

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: The effects of a large array on page load times and reliability

Posted 12 April 2012 - 04:08 AM

Hey, my question is why did you choose to use Javascript for this? This is better suited for server-side languages where caching can be easily done.
Was This Post Helpful? 0
  • +
  • -

#3 MrSooty  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 85
  • Joined: 14-September 11

Re: The effects of a large array on page load times and reliability

Posted 12 April 2012 - 04:16 AM

View Postcodeprada, on 12 April 2012 - 11:08 AM, said:

Hey, my question is why did you choose to use Javascript for this? This is better suited for server-side languages where caching can be easily done.


The web pages are distributed via CD, so there is no server side.
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: The effects of a large array on page load times and reliability

Posted 12 April 2012 - 05:31 AM

View PostMrSooty, on 12 April 2012 - 07:16 AM, said:

View Postcodeprada, on 12 April 2012 - 11:08 AM, said:

Hey, my question is why did you choose to use Javascript for this? This is better suited for server-side languages where caching can be easily done.


The web pages are distributed via CD, so there is no server side.

OK so basically you have to create a template system in Javascript?

Honestly speaking for this task there should be a server-side language implemented because there must be a web server if the site is online somewhere.

The page loading will take some time since you're dealing with a large amount of data. I can suggest you use a method called Memorization. It basically means you only load and store the data that is required. Let's say your page only uses 200 of the possible 5500 lines or definitions then you really don't need to load the other 5300 lines.
Here's some knocked up code that should better explain where I'm coming from.
var def = {};
var loadDef = function(name) {
	// get line from file and do your parsin
	// ... code
	return data;
}

var getDef = function(name) {
	//if we've not already used this definition then
	//load it from file and then add it to our def obj
	//in case it may be needed again
	if(def.name == undefined)
		def.name = loadDef(name);
	
	return def.name
}


Was This Post Helpful? 1
  • +
  • -

#5 MrSooty  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 85
  • Joined: 14-September 11

Re: The effects of a large array on page load times and reliability

Posted 12 April 2012 - 06:04 AM

They're static HTML pages with some Javascript weaved in sitting on a CD, so I guess it was wrong of me to call it a website. The user would put the CD in and browse through the pages as if it were a website.

As for templates: Yeah, that sounds like a good name for it.

So I guess what you're doing is, rather than load everything into 1 array and read from that array, you save everything into 1 array, copy definitions from array 1 to array 2 as you need them page loads and read from the second one, so if you return to a page after you've been to it before, you won't have to wait anywhere near the same amount of time to find the definitions?

Basically, caching?

I do quite like this :) Thanks for the help :)

This post has been edited by MrSooty: 12 April 2012 - 06:06 AM

Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: The effects of a large array on page load times and reliability

Posted 12 April 2012 - 07:30 AM

Oh well if the data is already hard-coded into an array you don't need to implement caching. Not much you can do to increase speed here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1