Split on first deliminator

Benchmark created on


Description

Test the performance of splitting a string only on the first delimitor

Setup


function test_single(path) {
	
	let component, last;
	
	while (path[0] === '/') path = path.slice(1);
	
	for (;;) {
		
		last = path;
		
		[component, path] = split(path);
		
		if (!path) break;
	}
}

const test_paths = [
	'/home/test/src/project/deps/libsomething/dist/file.js',
	'//net/path/with/oops///slashes'
];


for (const p of test_paths) test_single(p);

Test runner

Ready to run.

Testing in
TestOps/sec
Split and Join
function split(p) {
	const parts = p.split('/').filter(v => v);
	const first = parts.shift();
	return [first, parts.join('/')]
}
ready
Do/While Loop
function split(p) {
	let len = 0, c = p[0], last = '';
	
	do {
		c = name[++len];
		last += c;
	} while (c && c !== '/');
	
	return [last, p.slice(len)];
}
ready
Regex
const pattern = /^\/*([^\/]*)\/*(.*)$/;


function split(p) {
	const match = pattern.exec(p);
	match.shift();
	return match;
}
ready

Revisions

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