的Array.push(元)与阵列[array.length] =元素 [英] array.push(element) vs array[array.length] = element

查看:132
本文介绍了的Array.push(元)与阵列[array.length] =元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有一个理由去选择

 的Array.push(元素)

 数组[array.length] =元素

,反之亦然。

下面是一个简单的例子在那里我有数字数组,我想使这些新阵数字乘以2:

  VAR数= [5,7,20,3,13]。变种ARR1 = [];
VAR LEN = numbers.length;
对于(VAR I = 0; I< LEN,我++){
    arr1.push(数字[I] * 2);
}
警报(ARR1);变种ARR2 = [];
对于(VAR I = 0; I< LEN,我++){
    ARR2 [arr2.length] =号[I] * 2;
}
警报(ARR2);


解决方案

与当前的JavaScript技术,做到这一点,同时使用最少code最快的方​​法,是先存储的最后一个元素,从而分配全套数组索引,然后向后计数为0,而存储的元素,因而可以充分利用附近的记忆存储位置和减少高速缓存未命中。

  VAR ARR3 = [];
对于(VAR I = LEN; I> 0;){
    一世 - ;
    ARR2 [I] =号[I] * 2;
}
警报(ARR2);

请注意,如果被存储元件的数量是在JavaScript引擎的视图足够大,则阵列将被作为稀疏阵列创建并从不转换为一个常规平面阵列。

是的,我可以支持这一行动。唯一的问题是,JavaScript的优化是丢掉未使用的计算非常积极。因此,为了对结果进行比较计算,所有的结果都被存储(暂时地)。我认为是过时了,但实际上一个进一步的优化提高了速度进一步是pre-初始化使用新的Array(*长*)数组。这是一个古老的帽子戏法那一阵子没有什么区别,但在极端的JavaScript引擎优化的日子里,它似乎再有所作为。

 <脚本>
功能arrayFwd(套){
变种X = [];
对于(VAR I = 0; I< set.length;我++)
X [x.length] =设置[I]
返回X;
}功能arrayRev(套){
变种x =新的Array(set.length);
为(变量I = set.length;我大于0){
一世 - ;
X [i] =设置[I]
}
返回X;
}功能arrayPush(套){
变种X = [];
对于(VAR I = 0; I< set.length;我++)
x.push(集[I]);
返回X;
}结果= []; / *我们将存储结果,使
优化没有认识的结果,不使用
从而跳过功能的工作完全* /
功能定时器(F,N){
复位功能(X){
VAR N1 =新的Date(),I = N;
做{results.push(F(X)); }而(我 - 大于0); //这里做什么
回报(新的Date() - N1)/ N;
};
}设定= [];
对于(i = 0; I< 4096;我++)
将[I] =(ⅰ)*第(i + 1)/ 2;计时器= {
前锋:定时器(arrayFwd,500)
落后:定时器(arrayRev,500)
推:定时器(arrayPush,500)
};
对(在定时器K){
的document.write(K,'=',定时器[K](套),'MS< BR />');
}
< / SCRIPT>

歌剧12.15:

向前= 0.12毫秒
落后= 0.04毫秒
推= 0.09毫秒

铬(最新,V27):

转发= 0.07毫秒
落后= 0.022毫秒
推= 0.064毫秒

(用于比较的,不存储结果时,铬产生这些数字:
前锋= 0.032毫秒
落后= 0.008毫秒
推= 0.022毫秒

这是快了近四倍与做阵列前锋,和更快的与做俯卧撑近三倍。)

IE 10:
前锋= 0.028毫秒
落后= 0.012毫秒
推= 0.038毫秒

奇怪的是,火狐仍显示为更快推动。必须有一些code重写当推时,因为访问属性和调用一个函数是与Firefox引擎盖下回事均比纯,没有增强的JavaScript性能方面使用数组索引慢。

I was wondering if there is a reason to choose

array.push(element)

over

array[array.length] = element

or vice-versa.

Here's a simple example where I have an array of numbers and I want to make a new array of those numbers multiplied by 2:

var numbers = [5, 7, 20, 3, 13];

var arr1 = [];
var len = numbers.length;
for(var i = 0; i < len; i++){
    arr1.push(numbers[i] * 2);
}
alert(arr1);

var arr2 = [];
for(var i = 0; i < len; i++){
    arr2[arr2.length] = numbers[i] * 2;
}
alert(arr2);

解决方案

The fastest way to do it with current JavaScript technology, while also using minimal code, is to store the last element first, thereby allocating the full set of array indices, and then counting backwards to 0 while storing the elements, thereby taking advantage of nearby memory storage positions and minimizing cache misses.

var arr3 = [];
for (var i = len; i>0;){
    i--;
    arr2[i] = numbers[i] * 2;
}
alert(arr2);

Note that if the number of elements being stored is "big enough" in the view of the JavaScript engine, then the array will be created as a "sparse" array and never converted to a regular flat array.

Yes, I can back this up. The only problem is that JavaScript optimizers are extremely aggressive in throwing away calculations that aren't used. So in order for the results to be calculated fairly, all the results have to be stored (temporarily). One further optimization that I believed to be obsolete, but actually improves the speed even further is to pre-initialize the array using new Array(*length*). That's an old-hat trick that for a while made no difference, but no in the days of extreme JavaScript engine optimizations, it appears to make a difference again.

<script>
function arrayFwd(set) {
var x = [];
for (var i = 0; i<set.length; i++)
x[x.length] = set[i];
return x;
}

function arrayRev(set) {
var x = new Array(set.length);
for (var i = set.length; i>0;) {
i--;
x[i] = set[i];
}
return x;
}

function arrayPush(set) {
var x = [];
for (var i = 0; i<set.length; i++)
x.push(set[i]);
return x;
}

results = []; /* we'll store the results so that
optimizers don't realize the results are not used
and thus skip the function's work completely */
function timer(f, n) {
return function(x) {
var n1 = new Date(), i = n;
do { results.push(f(x)); } while (i-- > 0); // do something here
return (new Date() - n1)/n;
};
}

set = [];
for (i=0; i<4096; i++)
set[i] = (i)*(i+1)/2;

timers = {
forward: timer(arrayFwd, 500),
backward: timer(arrayRev, 500),
push: timer(arrayPush, 500)
};
for (k in timers) {
document.write(k, ' = ', timers[k](set), ' ms<br />');
}
</script>

Opera 12.15:

forward = 0.12 ms backward = 0.04 ms push = 0.09 ms

Chrome (latest, v27):

forward = 0.07 ms backward = 0.022 ms push = 0.064 ms

(for comparison, when results are not stored, Chrome produces these numbers: forward = 0.032 ms backward = 0.008 ms push = 0.022 ms

This is almost four times faster versus doing the array forwards, and almost three times faster versus doing push.)

IE 10: forward = 0.028 ms backward = 0.012 ms push = 0.038 ms

Strangely, Firefox still shows push as faster. There must be some code re-writing going on under the hood with Firefox when push is used, because accessing a property and invoking a function are both slower than using an array index in terms of pure, un-enhanced JavaScript performance.

这篇关于的Array.push(元)与阵列[array.length] =元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆