Repository navigation
Add Buffer.prototype.lastIndexOf() #4604
Description
Activity
I believe that comes from the
Uint8Arrayprototype, notBufferitself likeindexOf()does.EDIT:
> Buffer.prototype.lastIndexOf === Uint8Array.prototype.lastIndexOf; true > Buffer.prototype.indexOf === Uint8Array.prototype.indexOf; false> Uint8Array.prototype.indexOf [Function: indexOf] > Uint8Array.prototype.lastIndexOf [Function: lastIndexOf]It also has the same method signature: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/indexOf
- addedbufferIssues and PRs related to the buffer subsystem.Issues and PRs related to the buffer subsystem.docIssues and PRs related to Node.js documentation.Issues and PRs related to Node.js documentation.
on Jan 10, 2016 @felixfbecker I think Buffer's
indexOf()is a custom implementation though, it doesn't use Uint8Array?@felixfbecker Presumably because implementations for
uint8Arrray.indexOf()aren't necessarily fast?buffer.indexOf()uses Boyer Moore, I'm not sure what v8 uses for theiruint8Array.indexOf()implementation.I'm not familiar with the different algorithms but why has
indexOfthen a special implementation andlastIndexOfdoesn't?@felixfbecker Probably because nobody has created a PR to add
lastIndexOf()yet.- changed the title
[-]Document Buffer.prototype.lastIndexOf()[/-][+]Add Buffer.prototype.lastIndexOf()[/+]on Jan 10, 2016 @mscdex thanks for the explanation. I would say this isnt a high priority, but it's confusing when browsing the API docs to see the
indexOfmethod documented like it was special toBuffereven though it has the exact same API as theUint8Array, just different implementation.The implementation of
buffer.indexOfis different fromunit8array.indexOf.Uint8Array.prototype.indexOfcan only search for individual elements (i.e. numbers).Buffer.prototype.indexOfcan search for string, Buffer or numbers, so it's more similar toString.prototype.indexOfthanUint8Array.prototype.indexOf.- addedgood first issueIssues that are suitable for first-time contributors.Issues that are suitable for first-time contributors.
on Jan 11, 2016 Does documentation for this belong in the buffer API file? (https://github.com/emars/node/blob/master/doc/api/buffer.markdown). I found that the method does not work to search for Sub-Buffers or Strings(returns -1 in both cases), only numbers. Seems kinda buggy to put in the core docs. Any thoughts?
@emars That's probably worth creating a new issue for.
I would like to take this up if no one else is already on it.
Edit: What do these
byteOffsetchecks signify?So I have two ways of looking at the implementation for this:
- Call
indexOfString, etc with a reversed copy of array pointed to bythisand include adjustedoffset(dunno what the adjustment is as yet) - Implement
indexOfNumberFromEnd, etc which use something likememrchrand then buildlastIndexOfin terms of 'em
Option one obviously has a big perf hit and two involves either implementing new functions or passing flags to existing ones to tell which end to search from.
Which sounds better? Is there another approach that anyone has in mind?
- Call
@zeusdeux sorry, just saw your posts. just posted a PR with a patch. i started working on it on a few days ago on an island in the middle of nowhere :)
the PR is a work in progress: interface and tests, boyer moore not done. in short, it works but it's slow
Oh hey, this was implemented in 6c1e5ad!

The docs only mention
indexOf, but it's there, I would like to use it and don't feel good using undocumented API