简体   繁体   English

php与javascript混合时的性能和缓存

[英]performance and caching when mixing javascript with php

After doing a lot of reading on the subject, I realized that many developers mix javascript and php in the same file (by adding the .php extension or using other ways). 在对该主题进行了大量阅读之后,我意识到许多开发人员在同一文件中混合了javascript和php(通过添加.php扩展名或使用其他方式)。

On the other hand, if I choose to separate the javascript from the php and store it in an external cacheable static file, I gain some performance advantage, but I also need to find creative ways to pass server-side data to the javascript. 另一方面,如果我选择将javascript与php分开并将其存储在外部可缓存的静态文件中,则可以获得一定的性能优势,但是我还需要寻找创造性的方式将服务器端数据传递给javascript。

For example, since I can't use a php foreach loop in the .js file I need to convert php arrays to json objects using json_encode. 例如,由于我无法在.js文件中使用php foreach循环,因此需要使用json_encode将php数组转换为json对象。 In other cases, I need to declare gloabl javascript variables in the original php file so I can use them in the external js file. 在其他情况下,我需要在原始php文件中声明gloabl javascript变量,以便可以在外部js文件中使用它们。 Since server side processing is considered faster than javascript, converting to js arrays and using global vars may also be a bad idea... 由于服务器端处理的速度比javascript快,因此转换为js数组并使用全局var可能也是个坏主意...

The bottom line is I'm trying to understand the trade off here. 底线是我想在这里理解权衡。 Which has more impact on performance, enable caching of js files or keeping a cleaner code by avoiding global js variables and multidemnsional js arrays? 通过避免全局js变量和多维度js数组,哪个对性能有更大影响,启用js文件缓存或保持更干净的代码?

are you talking about performance of the server or the browser? 您是在谈论服务器或浏览器的性能吗?

my personal opinion is that given the choice between making a server slower or making a browser slower, you should always choose to let the browser be slower. 我个人的看法是,在选择让服务器变慢还是让浏览器变慢之间进行选择,您应该始终选择让浏览器变慢。

usually, "slow" means something like "takes 100ms" or so, which is not noticeable on an individual browser, but if you have a few hundred requests to a server and they're all delayed by that, the effect is cumulative, and the response becomes sluggish. 通常,“慢”表示类似“花费100毫秒”之类的内容,这在单个浏览器上并不明显,但是如果您对服务器有几百个请求,并且这些请求都被延迟,则效果是累积的,并且反应变得迟钝。 very noticeable. 非常引人注目。

let the browser take the hit. 让浏览器获得成功。

I think it depends on what your trying to do. 我认为这取决于您的尝试。 My personal opinion is that it's a little bit of a pain to prevent your dynamic JavaScript from being cached. 我个人的观点是,防止缓存动态JavaScript有点麻烦。

Your static JS files need to contain your functions and no dynamic data. 您的静态JS文件需要包含您的函数,而无需包含动态数据。 Your HTML page can contain your dynamic data. 您的HTML页面可以包含您的动态数据。 Either within a SCRIPT block (where you will be able to use PHP foreach), or by putting your data into the DOM where the JavaScript can read it, it can be visible (in a table) or invisible (eg in a comment) - depends on whether your data is presentable or not. 在SCRIPT块中(您将可以使用PHP foreach),或者将数据放入DOM(JavaScript可以读取它)中,可以将其可见(在表中)或不可见(例如在注释中)-取决于您的数据是否可显示。

You could also use AJAX to fetch your dynamic data, but this will be an additional request, just like an external JS file containing the data would. 您也可以使用AJAX来获取动态数据,但这将是一个附加请求,就像包含数据的外部JS文件一样。

As Kae says, adding additional load onto the client would benefit your server in terms of scalability (how many users you can serve at any one time). 正如Kae所说,在客户端上增加额外的负载将使您的服务器在可伸缩性(一次可以服务多少个用户)方面受益。

Data: 数据:

If the amount of dynamic data isn't too big and constantly changing (must not be cached by the browser), I would suggest adding it to the head of the HTML. 如果动态数据量不是太大并且不断变化(必须不被浏览器缓存),我建议将其添加到HTML的头部。 To prevent it from polluting the global namespace, you can use either a closure or a namespace (object), to contain all related variables. 为了防止它污染全局名称空间,可以使用闭包或名称空间(对象)来包含所有相关变量。 Performance-wise, I don't think that in this case, there would be much difference between looping the data into JS-friendly format or handling it to the finest detail on the server (JS has become amazingly fast). 在性能方面,我认为在这种情况下,将数据循环为JS友好格式或将其处理为服务器上最详细的细节(JS变得非常快)之间没有太大的区别。

Things are a bit more complicated when the amount of data is huge (100+kbs to megabytes). 当数据量巨大时(100 + kbs到兆字节),事情会变得更加复杂。 In case the data is pretty much constant and cacheable, you should generate a external data file (not an actual new file, but an unique URL), which you could then include. 如果数据非常恒定且可缓存,则应生成一个外部数据文件(不是实际的新文件,而是唯一的URL),然后可以将其包括在内。 Using a timestamp in the name or correctly set cache headers would then enable you to save the time on both server-side (generating the JS-friendly output) and client-side (downloading data) and still offer up to date data. 在名称中使用时间戳或正确设置缓存头将使您能够节省服务器端(生成JS友好的输出)和客户端(下载数据)的时间,并且仍提供最新数据。

If you have a lot of data, but it's constantly changing, I'd still use external JS files generated by PHP, but you have to be extra careful to disable browser caching, which make your constantly changing data pretty much constant. 如果您有大量数据,但是它在不断变化,那么我仍将使用PHP生成的外部JS文件,但是您必须格外小心,以禁用浏览器缓存,这会使您不断变化的数据变得非常稳定。 You could also do dynamic loading, where you pull different parts of data in parallel and on demand via JS requests. 您还可以进行动态加载,在这种情况下,您可以通过JS请求按需并行地提取数据的不同部分。

Code: 码:

The functional part of your code should follow the explanation from before: Now to the question whether JS should be inlined to the HTML or separated. 代码的功能部分应遵循之前的解释:现在是JS应该内联到HTML还是分开的问题。 This depends highly on code, mostly of it's length and reusability. 这在很大程度上取决于代码,主要取决于代码的长度和可重用性。 If it's just 20 lines of JS, 10 of which are arrays, etc that are generated by PHP, it would make more sense to leave the code inside the HTML, because HTTP requests (the way how all resources are delivered to the client) are expensive and requesting a small file isn't necessarily a great idea. 如果只有20行JS,其中10行是数组等,是由PHP生成的,那么将代码保留在HTML内会更有意义,因为HTTP请求(所有资源如何传递到客户端的方式)是昂贵并且请求一个小文件不一定是个好主意。

However, if you have a bit bigger file with lots of functionality etc (10s of kbs), it would be sensible to include it as a separate .js file in order to make it cacheable and save it from being downloaded every time. 但是,如果您的文件较大,并且具有许多功能(10 kbs),那么将其作为单独的.js文件包含在内是明智的,以便使其可缓存并避免每次下载。

And there's no difference in PHP or JS performance, whether you include the JS inside templates/PHP or separately. 而且,无论您将JS包含在模板/ PHP内还是单独包含在内,PHP或JS的性能都没有区别。 It's just a matter of making a project manageable. 这只是使项目易于管理的问题。 Whatever you do, you should seriously look into using templates. 无论您做什么,都应该认真考虑使用模板。

After doing a lot of reading 经过大量阅读

That's what you are probably doing wrong. 那就是你可能做错了。

There are many people fond on writing articles (and answers on Stackovervlow as well) who has a very little experience and whose knowledge is based... on the other articles they read! 有很多人喜欢撰写文章(也有关于Stackovervlow的答案),他们经验很少,其知识是基于...阅读的其他文章!
Don't follow their bad example. 不要遵循他们的坏榜样。

Instead of "a lot of reading" you have to do a lot of profiling! 代替“大量阅读”,您必须进行大量分析! .

First of all you have to spot the bottlenecks and see if any of them are caching related. 首先,您必须找出瓶颈,并查看它们是否与缓存相关。
Next thing you have to decide is what kind caching your system require. 接下来需要决定的是系统需要哪种缓存。
And only then you can start looking for the solution. 只有这样,您才可以开始寻找解决方案。

Hope it helps. 希望能帮助到你。

QDF to your problem is to send the data in a hidden HTML table. QDF给您的问题是在隐藏的HTML表中发送数据。

HTML tables are easy to generate in php and easy to ready in JavaScript. HTML表很容易在php中生成,并且很容易在JavaScript中准备。

I have a solution when the situation is passing info from php to js and keep most js outside the main php file. 当情况从php向js传递信息并将大多数js保留在主要php文件之外时,我有一个解决方案。 Use the js objects or js functions. 使用js对象或js函数。 You make some code that needs data from php. 您编写一些需要php数据的代码。 When the page loads some small js code is generated from php. 当页面加载时,会从php生成一些小的js代码。 Like: 喜欢:

<script type="text/javascript">
a(param1, param2, param3)
</script>

and it's done. 完成了。 The server indicates param1, param2 and param3 directly in the code. 服务器直接在代码中指示param1,param2和param3。

The function is inside a .js file that is cached. 该函数位于已缓存的.js文件中。 With this you reduce the server's upload and the time for the page js to start. 这样,您可以减少服务器的上载以及启动页面js的时间。 The client's code is a bit slower but you win for the time to download and the server becomes faster. 客户端的代码稍慢一些,但是您赢得了下载时间,服务器变得更快了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM