String at position in string (v2)

Revision 2 of this benchmark created on


Setup

string = new Array(1000).fill("abcdef").join("");

Test runner

Ready to run.

Testing in
TestOps/sec
substr() ===
function matches(string, cursor, seeking) {
	return string.substr(cursor, seeking.length) === seeking;
}
matches(string, 3, "de");
matches(string, 3, "defg");
matches(string, 3, "x");
ready
check character by character, early exit
function matches(string, cursor, seeking) {
	if (cursor + seeking.length > string.length) { return false; }
	else {
		for (var i = 0; i < seeking.length; i++) {
  			if (string[cursor+i] !== seeking[i]) { return false; }
		}
	}
	return true;
}
matches(string, 3, "de");
matches(string, 3, "defg");
matches(string, 3, "x");
ready
slice() ===
function matches(string, cursor, seeking) {
	return string.slice(cursor, cursor + seeking.length) === seeking;
}
matches(string, 3, "de");
matches(string, 3, "defg");
matches(string, 3, "x");
ready
substring() ===
function matches(string, cursor, seeking) {
	return string.substring(cursor, cursor + seeking.length) === seeking;
}
matches(string, 3, "de");
matches(string, 3, "defg");
matches(string, 3, "x");
ready
regex.test()
function matches(string, cursor, seeking) {
	return new RegExp(".{"+cursor+"}"+seeking).test(string);
}
matches(string, 3, "de");
matches(string, 3, "defg");
matches(string, 3, "x");
ready
.indexOf() === cursor
function matches(string, cursor, seeking) {
	return string.indexOf(seeking, cursor) === cursor;
}
matches(string, 3, "de");
matches(string, 3, "defg");
matches(string, 3, "x");
ready

Revisions

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