Split on first deliminator (v4)

Revision 4 of this benchmark created on


Description

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

Setup

function test_single(path, split) {
	
	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'
];

function test(split) {
	for (const p of test_paths) test_single(p, split);
}

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('/')]
}

test(split);
ready
Do/While Loop
function split(p) {
	let len = 0, c = p[0], last = '';
	
	do {
		last += c;
		c = p[++len];
	} while (c && c !== '/');
	
	for (len; p[len] === '/'; len++);
	
	return [last, p.slice(len)];
}

test(split);
ready
Regex
const pattern = /^\/*([^\/]*)\/+(.*)$/;

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

test(split);
ready

Revisions

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