I custom coded a dynamic database linked (through asp.net) photo gallery in javascript which manages about 300 images. There is no redundant code, but it is about 500 lines long. Should I be splitting it into different .js files, or is the one .js file with 500 lines okay? Is there a best practice for ensuring that javascript files stay under a certain amount of lines of code?
Asked
Active
Viewed 312 times
1
-
1You can have as many lines of code as you want. Fewer files means less HTTP requests which result in faster page load. Splitting up your code into several files might make it easier to maintain your code. There are also tools which combine JS files together again. Do what suits you better. – Felix Kling Dec 29 '11 at 20:26
2 Answers
4
The number of lines of codes doesn't matter. From a speed perspective it's better to place it in one file, because that reduces the number of trips to the sever, and the browser can cache the file for all future pages that uses it.
It sounds like you haven't run a minimizer (like YUI Compressor) on the file to shrink the size. That is the next step I would take as it will reduce the file size and reduce the download time.

kemiller2002
- 113,795
- 27
- 197
- 251
1
For execution, one file is best, unless it is modular and you're using some modules elsewhere.
For coding, whatever is comfortable to read is best. If you can find your way around a 5000-line JS file (let's say, with intelligent use of folds, or such), a 5000-line file is fine.

Amadan
- 191,408
- 23
- 240
- 301
-
Great :) It is pretty dynamic so the whole thing should modulate well, I tried not to couple it to the page at all. What are folds? Thanks for the input! – Travis J Dec 29 '11 at 20:31
-
-
@TravisJ: Hehe, sorry, I automatically assume everyone uses vim :P Sucks to be you :) – Amadan Dec 29 '11 at 20:39
-
@TravisJ: Kidding aside - I know nothing of M$ toolset, but... would [this](http://visualstudiogallery.msdn.microsoft.com/872d27ee-38c7-4a97-98dc-0d8a431cc2ed/) help? If not, maybe [this](http://stackoverflow.com/questions/1921628/how-to-implement-regions-code-collapse-in-javascript) will. – Amadan Dec 29 '11 at 20:43