I'm porting some of my OpenGL code to WebGL and the fact that JavaScript doesn't have genuine arrays is sad. I can use Float32Array
's (and other other ArrayBuffer
types), but that doesn't seem to help performance.
我正在将一些OpenGL代码移植到WebGL,而且JavaScript没有真正的数组这一事实令人难过。我可以使用Float32Array(和其他其他ArrayBuffer类型),但这似乎没有帮助性能。
As an experiment to compare Array
vs Float32Array
vs Float64Array
performance, I timed bubble sort on 100000 floats to see if there was any difference:
作为比较Array vs Float32Array与Float64Array性能的实验,我在100000个浮点数上定时冒泡排序以查看是否存在任何差异:
function bubbleSort(array) {
var N = array.length;
for (var i = 0; i < N; i++)
for (var j = i; j < N-1; j++)
if (array[j] > array[j+1]) {
var tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
// var nums = new Array(100000); // regular 'JS' array
// var nums = new Float32Array(100000); // actual buffer of 32-bit floats
var nums = new Float64Array(100000); // actual buffer of 64-bit floats
for (var i = 0; i < nums.length; i++)
nums[i] = Math.random() * 1000;
bubbleSort(nums);
for (var i = 0; i < nums.length; i++)
console.log(nums[i]);
Not much difference. Really the compiler would need some static type information for the array
argument for bubbleSort
to really get decent performance. Are we just stuck with bad array performance in JS? Any way around this? Short of using ASM.js that is...
差别不大。实际上编译器需要为bubbleSort的数组参数提供一些静态类型信息才能真正获得不错的性能。我们刚刚在JS中遇到了糟糕的阵列性能吗?有什么方法吗?没有使用ASM.js ......
1 个解决方案
#1
3
You should see this answer: What is the performance of Objects/Arrays in JavaScript? (specifically for Google V8)
您应该看到这样的答案:JavaScript中对象/数组的性能如何? (专门针对Google V8)
In particular the following points:
特别是以下几点:
- Array.push( data ); is faster than Array[nextIndex] = data by almost 20 times over.
- V8 array writes are slightly faster than V8 reads
Array.push(数据);比Array [nextIndex] =数据快了近20倍。
V8阵列写入比V8读取略快
So yes, it looks like Array indexing is slow in JS. If it's true that array writes are faster than reads in performance test, then it is likely that the dynamic allocation isn't picky about where it puts the data (fastest first?), whereas when it comes to reading, that will be a lot of memory addresses to jump between and so will be far slower.
所以是的,看起来JS中的数组索引很慢。如果确实数组写入比性能测试中的读取更快,那么动态分配可能不是它放置数据的位置(最快的第一个?),而当读取时,这将是很多内存地址之间的跳转会慢得多。
I'd be interested to see if you declare an array using literal syntax [0, 1, 2, ..., 100000], would the performance be any better?
我有兴趣看看你是否使用文字语法[0,1,2,...,100000]声明一个数组,性能会更好吗?
(I'll set up a JSPerf if I have the time).
(如果我有时间的话,我会设置一个JSPerf)。
#1
3
You should see this answer: What is the performance of Objects/Arrays in JavaScript? (specifically for Google V8)
您应该看到这样的答案:JavaScript中对象/数组的性能如何? (专门针对Google V8)
In particular the following points:
特别是以下几点:
- Array.push( data ); is faster than Array[nextIndex] = data by almost 20 times over.
- V8 array writes are slightly faster than V8 reads
Array.push(数据);比Array [nextIndex] =数据快了近20倍。
V8阵列写入比V8读取略快
So yes, it looks like Array indexing is slow in JS. If it's true that array writes are faster than reads in performance test, then it is likely that the dynamic allocation isn't picky about where it puts the data (fastest first?), whereas when it comes to reading, that will be a lot of memory addresses to jump between and so will be far slower.
所以是的,看起来JS中的数组索引很慢。如果确实数组写入比性能测试中的读取更快,那么动态分配可能不是它放置数据的位置(最快的第一个?),而当读取时,这将是很多内存地址之间的跳转会慢得多。
I'd be interested to see if you declare an array using literal syntax [0, 1, 2, ..., 100000], would the performance be any better?
我有兴趣看看你是否使用文字语法[0,1,2,...,100000]声明一个数组,性能会更好吗?
(I'll set up a JSPerf if I have the time).
(如果我有时间的话,我会设置一个JSPerf)。