Open rentalhost opened 4 months ago
I think the idea is good but I would be wary straying farther from the standard definition of Uint8Array.prototype.indexOf. It would also be nice for this to work with all TypedArrays. So maybe a method on the buffer module instead? e.g. require('buffer').indexOf(buf, value, start, end)
. Either way, I'm +1 on the general idea
Is someone interested to work on this issue, or can I take this up?
I am -1 on this. It should be proposed to tsc39. Maybe also proposing this for normal Arrays.
What is the problem this feature will solve?
Currently, if you have a task where you need to search for a certain value in a
Buffer
, you will use.indexOf()
. However, if thisBuffer
is too long and you don't need to search very deeply (like in Yaz0 compression method), you will probably end up searching too deep unnecessarily.A primary solution is to use
.subarray()
to limit the maximum search capacity, however, creating aBuffer
in this process is much more costly for small and medium-sized files than using.indexOf()
even though it has to go to the end ofBuffer
.What is the feature you are proposing to solve the problem?
My proposal is to include the possibility of determining how far
.indexOf()
can search from the current point. The signature would be something like:Then, when the third argument is entered, the search will end if no
value
element is found in theBuffer
in time.Example:
What alternatives have you considered?
.subarray()
is unfeasible, due to the many times it needs to be executed;.indexOf()
works, and managed to be around 55% faster than the original method;So, supporting a limiter in
.indexOf()
seems interesting to me, mainly to avoid using.subarray()
to limit the scope of the search when the limit is known.