substring vs lastindexof (v2)

Revision 2 of this benchmark created on


Setup

let str = "azezrjzelxckqjksldjiazknqsklcjqsklqd"

let search = "lxc";
let end = 20;
let beg = 4

Test runner

Ready to run.

Testing in
TestOps/sec
lastIndexOf
let index = str.lastIndexOf(search, end)

if(index < beg )

   index = -1
ready
substr
str.substring(beg, end).lastIndexOf(search)
ready
by hand
let offset = search.lenght-1;

let idx = end
while( idx >= beg && offset >= 0 ) {
	
	if( search[offset] === str[i] ) {
		--offset;
		--idx;
	} else {
		idx += search.lenght-1-offset
		offset = search.lenght-1;
	}
}


if(offset < 0)

    idx = -1
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.