Using exceptions for control flow (v2)

Revision 2 of this benchmark created on


Setup

returnPlainly = () => {
	return 'A'
}

processUsingIfElse = () => {
	var code = returnPlainly()
	if (code === 'A')
		return 'A returned'
	else
		return 'A not found'
}

returnUsingThrow = () => {
	throw new Error('A')
}

processUsingTryCatch = () => {
	try {
		returnUsingThrow()
	} catch(e) {
		if (e.message === 'A')
			return 'A returned'
		else
			return 'A not found'
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
if-else
var code = 'A'
if (code === 'A')
	return 'A returned'
else
	return 'A not found'
ready
try-catch
try {
	throw new Error('A')
} catch(e) {
	if (e.message === 'A')
		return 'A returned'
	else
		return 'A not found'
}
ready

Revisions

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