最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Disadvantages of separating JavaScript Codes? - Stack Overflow

programmeradmin3浏览0评论

I have a page which has many event handlers. The code now reached 1000+ lines of codes and I'm beginning to have difficulty reading the codes. I'm now planning to separate the codes to different files. My question is, is there any disadvantages in separating JS codes into different files?

I have a page which has many event handlers. The code now reached 1000+ lines of codes and I'm beginning to have difficulty reading the codes. I'm now planning to separate the codes to different files. My question is, is there any disadvantages in separating JS codes into different files?

Share Improve this question asked Mar 27, 2012 at 5:20 kazinixkazinix 30.1k36 gold badges110 silver badges162 bronze badges 5
  • 4 One thing that es immediately to my mind is another network request. I would suggest to have a readable dev file or multiple files but one minified and or gzipped file for production. – Kumar Commented Mar 27, 2012 at 5:23
  • (Edit: Kumar beat me to it, but I think the lack of concurrent requests still holds.) The only significant one I can think of is that loading different JS files can be a bit slow. Since they can conflict, only one can load at a time (though browsers may have started working around that -- haven't checked in the past few years). Though you could always just glob some files together in the production build. – Corbin Commented Mar 27, 2012 at 5:23
  • agree with Kumar - although will say that you will not see that much of performance difference unless you have many more files with much more code. – dbrin Commented Mar 27, 2012 at 5:28
  • 3 I use a template engine, separating javascript in multiple files but the output is always one file – pylover Commented Mar 27, 2012 at 5:35
  • @pylover Template engine? Hm I'm using Visual Studio, is there any plugin for it? :) – kazinix Commented Mar 27, 2012 at 5:46
Add a ment  | 

6 Answers 6

Reset to default 4

The best practice is to create relatively small files for development purposes where each file contains a module of functionality that is all related (whatever is most efficient for development/debugging/editing/source control. You can give each file a meaningful name that describes what is in it. Each of these files can be managed separately (their own version history in your source control system, check in/check out, etc...). It's often easier to have multiple tabs open with separate files in your editor than trying to use bookmarks to jump around between different places in one large file, etc...

Then, when you go to deploy your app, you use a tool (like Google closure or YUI Compressor) to minimize and bine all your smaller files into one deployment file. This preserves the development advantages of smaller files that contain related code while preserving the deployment advantage of having most/all the code in one larger external javascript file that can be downloaded in one http request and can be cached very effectively.

disdvantages?

  • another HTTP request. developers advise that files be pressed and be in as few files as possible for optimization. the less files you request, the faster (since there is less round trips)

  • if you use a text-editor with an auto-plete, some of these editors won't pick-up the stuff from the other file for auto-plete. this can be a problem if you don't memoize the functions you have.

  • if one of these inter-dependent files failed to load, your app will break.

The disadvantage would be the added plexity of making sure you're including everything correctly. But 1000 lines is getting unwieldy. Here's a related page about calling JavaScript objects in separate files

In addition to my ment.

Use t4 templates from here, or you can use C# to write javascript by script#

in modern platforms, developers does not write javascript directly, for example

  • in Rails, using CoffeScript
  • in python, using one of (py2js,pyjamas,google V8, skulpt)
  • in C#, using script#

Here is a bolg post about JSminify and similar tools to use with VisualStudio and build automation: http://encosia./automatically-minify-and-bine-javascript-in-visual-studio/

Having code separate in different files helps you for maintenance. I will suggest to you to split it in modules patible with CommonJS, if you're working in a browser environment you can use RequireJS to load them. You can also use the utility provided to create a single file when you deploy your website / webapp in order to optimize the http requests.

发布评论

评论列表(0)

  1. 暂无评论