简体   繁体   English

大数据集的Javascript数组取消移位和弹出操作

[英]Javascript array unshift and pop operation for big dataset

I have a big array used to store numbers (size of 1000 or more), this array values will be used to render the y-axis line graph in real time. 我有一个用于存储数字(大小为1000或更大)的大数组,该数组值将用于实时呈现y轴折线图。

I will constantly unshift this array to add new data and pop to remove the last data in the array. 我会不断unshift这个数组中添加新的数据和pop删除阵列中的最后一个数据。 This operation allows my graph to move from left to right. 此操作允许我的图形从左向右移动。

However, this method of unshift and pop is easy, but it will cause memory fragmentation and time performance when my array size grows and i could have more than 1 graph, (n-th graph and each contain one of the big array). 但是,这种unshiftpop方法很简单,但是当我的数组大小增加并且我可能有多个图(第n个图,每个都包含一个大数组)时,它将导致内存碎片和时间性能。 May I know is there any better way to do this operation? 我可以知道是否有更好的方法可以执行此操作?

I was curious enough to measure shift/push against the traditional same-memory copying and the results are pretty clear that there's no optimization necessary. 我很好奇地测量了相对于传统的相同内存复制的移位/推动,结果很明显,没有必要进行优化。

 var p = [], q = []; for (var i = 0; i < 10000; i++) p[i] = i; for (var i = 0; i < 10000; i++) q[i] = i; function rotate_1(a, x) { var len = a.length; for (var i = 1; i < len; i++) a[i - 1] = a[i]; a[len - 1] = x; } function rotate_2(a, x) { a.shift(); a.push(x); } t = new Date() for (var i = 0; i < 100000; i++) rotate_1(p, i); document.write("copy="); document.write(new Date() - t); document.write("<br>"); t = new Date() for (var i = 0; i < 100000; i++) rotate_2(q, i); document.write("shift/push="); document.write(new Date() - t); document.write("<br>"); 

There are two sides of this problem: 此问题有两个方面:

Is there any better way to do this operation? 有没有更好的方法可以执行此操作?

It strongly depends on how do you draw the graph. 这在很大程度上取决于您如何绘制图形。 Do you use some kind of library for that? 您为此使用某种库吗? If so, it probably expects a specific data (eg. Array) so there isn't much you can do about it. 如果是这样,它可能需要特定的数据(例如数组),因此您无能为力。 On the other hand, if you're implementing the drawing part by yourself then you can use any data structure you want (including lists which could be suitable here). 另一方面,如果您要自己实现图形部分,则可以使用所需的任何数据结构(包括可能适合此处的列表)。

Memory fragmentation and Array performance 内存碎片和Array性能

First of all ask yourself a question: Do you actually have any performance problems with Array operations or is this just theoretical? 首先要问自己一个问题: Array操作实际上有性能问题吗?还是仅仅是理论上的问题?

Drawing the graph is far more time-consuming than any array operations. 绘制图形比任何数组操作都耗时得多。 I think you shouldn't worry about Array methods' performance unless you actually have any problems with them. 我认为除非您真的对Array方法有任何疑问,否则您不必担心Array方法的性能。 Mind that in JS engines Array s are not necessarily implemented as arrays . 请注意, 在JS引擎中, Array不一定要实现为数组 They might be binary search trees or even something else, depending on how you use them and therefore it's hard to predict which optimisations will work for your case and which will make things worse. 它们可能是二叉搜索树,甚至可能是其他东西,这取决于您如何使用它们,因此很难预测哪种优化将适合您的情况,哪些会使情况变得更糟。

Is there any way to increase performance? 有什么办法可以提高性能?

Yes. 是。 But it depends on how do you actually use the array. 但这取决于您如何实际使用数组。 I can imagine if you draw a live graph and unshift / pop data eg. 如果绘制现场图,我可以想像unshift / pop数据,例如。 every 1 second, you know exactly the length of the array, is that correct? 每隔1秒,您确切地知道数组的length ,对吗? It should remain constant. 它应该保持不变。 Therefore one of the things you could do is, at the beginning, allocate memory for an array of that length. 因此,您可以做的一件事就是在一开始就为该长度的数组分配内存。 Your memory shouldn't get fragmented if you do unshift + pop in the future because that's an easy case to optimise by the engine. 如果将来执行unshift + pop ,您的内存就不会变得零散,因为这是引擎优化的简单案例。 But then again, it's just guessing, because JavaScript engines are smarter than I am . 但话又说回来,这只是猜测,因为JavaScript引擎比我聪明 More hints and tricks are described in the article Let's get those Javascript Arrays to work fast . 让我们快速运行这些Javascript数组文章中描述了更多的提示和技巧。

Don't try to fix something that's not broken. 不要尝试修复未损坏的东西。 Measure before you start refactoring and don't overthink. 开始重构之前先进行测量,不要过分思考。 This is called premature optimisation and I'm positive that Array operations won't be any priority to optimise when you actually have the application running. 这称为过早优化,我很肯定在实际运行应用程序时,阵列操作不会成为优化的优先事项。

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

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