Because I keep needing to do it, I have become irritated by the lack of a function in the typed array specification do copy the partial contents of a JS source array, something like:
void set(type array, optional unsigned long offset, unsigned long srcOffset, unsigned long count)
The obvious answer is a wrapper but I suspected that a loop of, e.g, i16array[i] = src[i] in JS would be slower than something internal to the typed array implementation. I wrote a short test for this. The result on FF4 is as I expected:
Int16Array.set(2000 byte array) x 100 times took 1ms (400000000 bytes/second).
Int16Array[j] = data[j] 2000 x 100 times took 4ms (100000000 bytes/second).
Int16Array.set(200000 byte array) x 1 times took 1ms (400000000 bytes/second).
Int16Array[j] = data[j] 200000 x 1 times took 4ms (100000000 bytes/second).
To ensure the result is not influenced by smart optimizers the test is repeated with a longer array and a single iteration. The bytes copied is the same in each case.
The "wrapper" runs at one quarter the speed of a native implementation so I think the above described set function is a badly needed addition to typed arrays.
If the source is a typed array, one can always create another view or subarray so it is not so important to add a new function for this case, though there is the issue of the garbage that must then be collected.
When I ran the same test on Chromium 12.0.717.0 (79525) I got a very surprising result:
It is surprising for 3 reasons:
The test is attached.
- the overall poor performance
- the fact that the Int16Array.set takes longer than a JS loop setting individual elements
- the fact that a single loop of 200,000 setting individual elements took 4 times longer than a double loop of 100 x 2000.