What is faster and why?

℡╲_俬逩灬. 提交于 2019-12-11 07:06:21

问题


For maintainability reasons, I want to database drive my javascript, in that I only want to send the javascript which is needed based on the users options.

So, is it faster/less resource heavy to have links in the database pointing to javascript files, then using response.writefile to embed those files into the clientside page, or is it faster/less resource heavy to stick the javascript script straight into the database, and response.write it onto the screen as and when needed?


回答1:


So, is it faster/less resource heavy to have links in the database pointing to javascript files

Usually yes.

External Javascript files can be cached by the browser, and will be loaded only once. Javascript code injected into the HTML page needs to be loaded every time, which increases bandwidth and loading times.

Also, having JavaScript code in a static file (instead of a dynamic one that fetches the code from the database on every request) is bound to be the fastest and least resource-intensive solution.




回答2:


  1. Don't use Response.Write.
  2. Be aware that if you send the entire JS file once, the client will / should cache it so it doesn't have to be sent again.
  3. DB lookups for every page just to get the relevant JS will be slow.



回答3:


By only sending the links to the javascript to the client a seperate HTTP request will have to be created in order to go and get each file.

If you embed only the required javascript directly into the page when needed this prevents this. Look at jQuery, thats got a load of javascript available to the client that isn't being used.

Less HTTP requests generally results in a faster website so I would go with embedding the code directly.

Edit:

I disagree with the caching answers above. If you ensure only the smallest amount of javascript that is required is embedded then my answer should be faster. This page will be cached too!



来源:https://stackoverflow.com/questions/3829801/what-is-faster-and-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!