简体   繁体   English

减少请求的文件大小或减少浏览器计算的数量?

[英]Reduce requested file size or reduce number of browser calculations?

I have some data that I want to display on a web page. 我有一些要显示在网页上的数据。 There's quite a lot of data so I really need to figure out the most optimized way of loading and parsing it. 有很多数据,所以我真的需要找出最优化的加载和解析方法。 In CSV format, the file size is 244K, and in JSON it's 819K. CSV格式的文件大小为244K,JSON格式的文件大小为819K。 As I see it, I have three different options: 如我所见,我有三种不同的选择:

  1. Load the web page and fetch the data in CSV format as an Ajax request . 加载网页并作为Ajax请求CSV格式获取数据。 Then transform the data into a JS object in the browser (I'm using a built-in method of the D3.js library to accomplish this). 然后在浏览器中将数据转换为JS对象(我正在使用D3.js库的内置方法来完成此操作)。
  2. Load the web page and fetch the data in JSON format as an Ajax request . 加载网页并作为Ajax请求JSON格式获取数据。 Data is ready to go as is. 数据准备就绪。
  3. Hard code the data in the main JS file as a JS object. 将主JS文件中的数据硬编码为JS对象。 No need for any async requests. 无需任何异步请求。

Method number one has the advantage of reduced file size, but the disadvantage of having to loop through all (2700) rows of data in the browser. 方法一的优点是减小了文件大小,但缺点是必须循环浏览器中的所有(2700)行数据。 Method number two gives us the data in the end-format so there's no need for heavy client-side operations. 第二种方法为我们提供了最终格式的数据,因此不需要繁琐的客户端操作。 However, the size of the JSON file is huge. 但是,JSON文件的大小很大。 Method number three has the advantage of skipping additional requests to the server, with the disadvantage of a longer initial page load time. 方法三的优点是跳过对服务器的其他请求,而缺点是初始页面加载时间较长。

What method is the best one in terms of optimization? 在优化方面,哪种方法最好?

In my experience, data processing times in Javascript are usually dwarfed by transfer times and the time it takes to render the display. 以我的经验,Javascript中的数据处理时间通常与传输时间和呈现显示所花费的时间相形见war。 Based on this, I would recommend going with option 1. 基于此,我建议选择选项1。

However, what's best in your particular case really does depend on your particular case -- you'll have to try. 但是,实际情况中的最佳选择确实取决于您的具体情况-您必须尝试。 It sounds like you have all the code/data you need to do that anyway, so why not run a simple experiment to see which one works best for you. 听起来您已经拥有执行此操作所需的所有代码/数据,所以为什么不运行简单的实验以查看哪个最适合您。

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

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