将常量作为参数传递的 JavaScript 效率?

发布于 2024-10-18 05:33:00 字数 548 浏览 1 评论 0原文

我有一个一般性的 JavaScript 问题。我会给你我的场景,然后问你问题。

场景

我正在制作一个表,其中(当前)超过 3000 行,并且每天都在增长 5-10 行。我正在使用 javascript 插件来设计该表的样式并添加有用的功能。目前完全加载页面需要 15 秒,之后一切都会顺利运行(排序、分页等)。不过,这是一个非常慢的初始加载。该插件提供了一种更少 DOM 解析的方法,您可以向它传递一个要放置在表中的信息数组,我对此非常感兴趣。但是,我希望尽可能快地完成此操作,因为仍然存在 3000 行的数组(每行 11 列,平均 10 个字符)。

问题

使用 JavaScript const 来存储这个巨大的数组会明显更快吗?具体来说,JavaScript 是否知道在作为参数传递时不将 const 放入堆栈中?

此外,这对 JavaScript 来说是不是太难以处理了?我是否应该放弃这个想法并立即开始使用 AJAX(这意味着功能要慢得多,但页面加载要快得多)?

谢谢!

I have a general JavaScript question. I'll give you my scenario and then ask you the question.

Scenario

I am making a table with (currently) over 3000 rows, and it is growing by 5-10 every day. I am using a javascript plugin to style this table and add useful functionality. It currently takes 15 seconds to fully load the page, after which everything runs smoothly (sorting, paging, etc.). That is a very slow initial load, though. The plugin offers a way with less DOM parsing, where you pass it an array of the information to be placed inside the table, which I am very intrigued by. However, I want to make this as fast as possible, because there will still be an array of 3000 rows (each with 11 columns of, on average, 10 characters).

Question

Would it be significantly faster to use a JavaScript const to store this giant array? Specifically, does JavaScript know not to put a const on the stack when passed as a parameter?

Furthermore, is this simply too much for JavaScript to handle? Should I dismiss this idea and start with AJAX now (which would mean much slower functionality but much faster pageload)?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧竹 2024-10-25 05:33:00

因为你说页面加载后交互速度很快,我猜你最大的瓶颈是通过网络传输数据。

我会将所有内容作为 JSON 发送(使用 gzip< 压缩) /a>),它非常轻量并且加载快速

我认为样式应该用CSS而不是JS来完成。另外,如果您想要最好的用户体验,请初始化您的表格,减少(1-200 个元素),然后处理其余的。如果一开始就展示正确的内容,对用户来说会更好。

存储数组不会有问题,因为 GC 会将其清除。

Because you say interaction is fast once the page is loaded I guess your biggest bottleneck is transfering the data over the wire.

I would send everything as JSON (compressed with gzip) which is very lightweight and fast to load.

I think styling should be done with CSS not JS. Also if you want the best UX initialize your table with less (1-200 elements), and then deal with the rest. It is better for the user if you show something right at the beginning.

Storing the array can't be a problem because GC will clear it up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文