What is the fastest method to calculate substring

Posted by Misha Moroshko on Stack Overflow See other posts from Stack Overflow or by Misha Moroshko
Published on 2010-05-28T13:36:34Z Indexed on 2010/05/28 13:42 UTC
Read the original article Hit count: 168

Filed under:

I have a huge "binary" string, like: 1110 0010 1000 1111 0000 1100 1010 0111....

It's length is 0 modulo 4, and may reach 500,000.

I have also a corresponding array: {14, 2, 8, 15, 0, 12, 10, 7, ...}

(every number in the array corresponds to 4 bits in the string)

Given this string, this array, and a number N, I need to calculate the following substring string.substr(4*N, 4), i.e.:

for N=0 the result should be 1110

for N=1 the result should be 0010

I need to perform this task many many times, and my question is what would be the fastest method to calculate this substring ?

One method is to calculate the substring straight forward: string.substr(4*N, 4). I'm afraid this one is not efficient for such huge strings.

Another method is to use array[N].toString(2) and then wrap the result with zeros if needed. I'm not sure how fast is this.

May be you have any other ideas ?

© Stack Overflow or respective owner

Related posts about JavaScript