slice() regex vs manually (v3)

Revision 3 of this benchmark created on


Setup

let acc1 = "";
let acc2 = "";
let acc3 = "";
let acc4 = "";
let acc5 = "";

const regex1 = /[\w]+/y;
const regex2 = /[^\w]/y;
const regex3 = /[\w]/y;

Test runner

Ready to run.

Testing in
TestOps/sec
Test1
for(let i = 0; i < 1000; ++i) {
	let str = ` ${i} `;
	let j = 1;
	let char = str.codePointAt(j);
	while( char >= 97 && char <= 122 || char >= 65 && char <= 90 || char >= 48 && char <= 57 || char === '_' )
	    char = str.codePointAt(++j);
	acc1 += str.slice(1,j);
}
ready
Test2
for(let i = 0; i < 1000; ++i) {
	let str = ` ${i} `;
	regex1.lastIndex = 1;
	acc2 += regex1.exec(str);
}
ready
Test3
for(let i = 0; i < 1000; ++i) {
	let str = ` ${i} `;
	let j = 1;
	regex2.lastIndex = j;
	while( ! regex2.test(str) )
	    regex2.lastIndex = ++j;
	acc3 += str.slice(1,j);
}
ready
Test4
for(let i = 0; i < 1000; ++i) {
	let str = ` ${i} `;
	let j = 1;
	regex3.lastIndex = j;
	while( regex3.test(str) )
	    regex3.lastIndex = ++j;
	acc4 += str.slice(1,j);
}
ready
Test5
for(let i = 0; i < 1000; ++i) {
	let str = ` ${i} `;
	acc5 += str.slice(1, str.slice(1).search(regex2) );
}
ready

Revisions

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