Javascript - Fastest way of copying an array portion into another?

You could keep unrolling the loop for even slighter increases in performance, but it seems like this is just about as fast as you're going to get it. As Gumbo stated in a comment, try using pre-increment instead of post-increment: var I = 0x4000>>5 + 1; // loops count var j = 0x4000 - 1; // write start index var k = 0x8000 - 1; // read start index while (--i) { // loop unrolling dst++j=src++k; dst++j=src++k; dst++j=src++k; dst++j=src++k; dst++j=src++k; dst++j=src++k; dst++j=src++k; dst++j=src++k; //8.

You could keep unrolling the loop for even slighter increases in performance, but it seems like this is just about as fast as you're going to get it. As Gumbo stated in a comment, try using pre-increment instead of post-increment: var I = 0x4000>>5 + 1; // loops count var j = 0x4000 - 1; // write start index var k = 0x8000 - 1; // read start index while (--i) { // loop unrolling dst++j=src++k; dst++j=src++k; dst++j=src++k; dst++j=src++k; dst++j=src++k; dst++j=src++k; dst++j=src++k; dst++j=src++k; //8 ...

Sep 19 '09 at 17:54 Right, missed that – Carson Myers Sep 19 '09 at 18:07.

Im not sure your method is faster than this: var I = 0x4000; // loops count var j = 0x4000; // write start index var k = 0x8000; // read start index while (i--) { // loop unrolling dstj++=srck++; }.

It is (tested in chrome, FF3.5 and Opera) Have a look at: codebase. Es/test/copytest. Htm – Pedro L.

Sep 19 '09 at 17:23.

I would consider the slice method: var dst = src. Slice(start,end) The performance depends on the implementation of javascript engine, but presumably all the browsers have implemented as efficient as possible on their platform. See more here.

He asks to replace contents of another array, not to form a new one. – Cem Kalyoncu Sep 19 '09 at 17:09 @cemkalyoncu: Then do slice and splice. – Gumbo?

Sep 19 '09 at 17:18 3 "Slice and splice" sounds like a TV direct-response product. "Tired of writing clumsy for loops? Try new Slice and Splice!

It slices, it splices, it takes the pain out of array processing. Order now for only $19.95! " – Matthew Crumley Sep 19 '09 at 17:35 slice + splice is much slower.

Look at copy 3: codebase. Es/test/copytest. Htm – Pedro L.

Sep 19 '09 at 17:36.

An alternative worth benchmarking may be building a brand new dst array with just a few powerful primitives rather than performing the loop, i.e. : dst = dst. Slice(0, writestart).

Concat( src. Slice(readstart, readstart+count), dst. Slice(writestart+count)); How the performance of this approach compares to looping will no doubt vary with the array lengths and counts involved, as well as with the underlying Javascript engine -- guessing would not be very productive, which is why I suggest benchmarking instead;-).

This is around 10 times slower – Pedro L. Sep 19 '09 at 17:34 Some internal functions which most probably written in C++ works slower than interpreting bunch of lines, strange. BTW I tested same code in C resulting 6-14ms of copy time (1000 x 0x4000 elements) – Cem Kalyoncu Sep 19 '09 at 17:43 modern browsers compile javascript into machine code.

For example Chrome's V8 and FF's TraceMonkey. – Pedro L. Sep 19 '09 at 17:45.

Try a combination with the build-in method slice and splice: Array.prototype.splice. Apply(dst, j, i. Concat(src.

Slice(k, k+i))).

Sorry. Your solution is around 50 times slower. – Pedro L.

Sep 19 '09 at 17:55 Not if you want it to perform well. – Derrick Apr 18 at 11:42.

You can create a completely unrolled version, eg via function compileCopy(count, di, si) { var sb = new Array(count); di += count, si += count; while(count--) sbcount = --di + '=src' + --si; return new Function('dst' + sb. Join(';dst') + ';'); } var copy = compileCopy(0x4000, 0x4000, 0x8000); In Opera, it will be slightly faster than the looping version. In FF... not (there might even be some bug somewhere).

But the start indexes are hardcoded into the "compiled" function. For each copy call you would need to "compile" it again. – Pedro L.

Sep 19 '09 at 19:11 your benchmark code has hardcoded indices as well – Christoph Sep 19 '09 at 19:19 but they could be function parameters without performance loss. – Pedro L. Sep 19 '09 at 19:52.

Sorry, one year later, but.. this is fastest in FF: function copy4() { var I = 0x4000; // loops count var j = 0x4000; // write start index var k = 0x8000; // read start index var args = src. Slice(k, k+i); args. Unshift(i); args.

Unshift(j); Array.prototype.splice. Apply(dst,args); }.

The performance depends on the implementation of javascript engine, but presumably all the browsers have implemented as efficient as possible on their platform. An alternative worth benchmarking may be building a brand new dst array with just a few powerful primitives rather than performing the loop, i.e. Dst = dst.

How the performance of this approach compares to looping will no doubt vary with the array lengths and counts involved, as well as with the underlying Javascript engine -- guessing would not be very productive, which is why I suggest benchmarking instead;-). Apply(dst, j, i. Return new Function('dst' + sb.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions