Javascript:将大数据集传递给多个函数

发布于 2024-11-02 02:51:31 字数 466 浏览 1 评论 0原文

我正在使用 YUI3 小部件,但对于你们 jquery 的人来说,只是假装我们正在谈论插件。我有一个很大的 json 数据集,其中有超过 5,000 条记录。在我的应用程序中,我至少有 5 个小部件需要访问我的数据集。目前,我将 json 嵌入 html 中,并在渲染小部件之前在页面加载时使用 YUI3 抓取它。

我想要避免的是在内存中存储 5 个数据集副本。我正在尝试想出一种设计模式来避免这种情况。我相信,如果我将此数据作为属性传递给我的小部件,它将按值传递,而不是引用,并且本质上将其复制到内存中......如果我对此有误,请告诉我。由于数据是全局可用的,我的小部件可以直接访问它,但我认为这也会复制内存中的数据。

我认为,如果我最初创建一个对象,将数据加载到该对象中并将该对象作为属性传递,它将通过引用传递它,这将允许我的所有小部件访问我的数据的同一实例。

这是你会做的吗?请让我知道您的想法以及您将如何设置这样的事情。

感谢您的(积极)投入!

I'm using YUI3 widgets, but for you jquery guys just pretend we are talking about plugins. I have a large json data set with over 5,000 records in it. In my applications I have at least 5 widgets that need access to my data set. Currently I'm embedding the json in the html and grabbing it with YUI3 on page load, before I render my widgets.

What I want to avoid is having 5 copies of my data set stored in memory. I'm trying to come up with a design pattern that allows me to avoid this. I believe if I pass this data in as an attribute to my widget, it will pass it by value, not reference and essentially copy it in memory ... let me know if I am wrong about this. Since the data is globally available, my widgets can access it directly, but I think this will also copy the data in memory.

I think if I initially create an object, loading my data in the object and pass that object in as an attribute it will pass it by reference, which will allow all of my widgets to access the same instance of my data.

Is this what you would do? Please let me know you thoughts and how you would set something like this up.

Thanks for your (positive) input!

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

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

发布评论

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

评论(2

梦晓ヶ微光ヅ倾城 2024-11-09 02:51:31

如果您将“数据集”保留在其自己的单独 .js 文件中,定义如下:

// My Huge Data Set
// Author: bababa
//
(function(window) {
  window['hugeDataSet'] = {
    // millions of lines of stuff
  };
})(this);

那么当您使用普通

someWidget.doSomething(hugeDataSet);

这不会复制数据。当然,您可以将数据内联放置在 HTML 的

If you keep the "data set" around in a separate .js file of its own, defined like this:

// My Huge Data Set
// Author: bababa
//
(function(window) {
  window['hugeDataSet'] = {
    // millions of lines of stuff
  };
})(this);

then when you import that with a plain <script> tag or with a script loader, there'll just be one global copy of the data. You can reference it like this:

someWidget.doSomething(hugeDataSet);

and that will not make a copy of the data. Of course, you can drop the data inline, in a <script> in your HTML, but then it won't be cached if your HTML is generated by a server-side template (like php or jsp or whatever).

紧拥背影 2024-11-09 02:51:31

您的小部件是否需要每次访问所有 5,000 条记录?或者他们只需要您数据的特定子集?

在我看来,您正在尝试使用 json 来替代数据库。

最可能的解决方案是将数据放入适当的数据库中,使用服务器端脚本请求所需的数据,并将这些数据 json 返回到您的小部件。

与数据库查询相比,解析文本非常慢。

Do your widget need to access all 5,000 records each time? Or do they only require a specific subset of your data?

It seems to me that you are trying to use json has an alternative for a database here.

The most likely solution is to put your data in a proper database, request the data you need with a server-side script and json these data back to your widget.

Parsing text is very slow compared to a database query.

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