Generators

Benchmark created on


Setup

function fib1(n) {
	if (n < 2)
		return 1;
	return fib1(n - 1) + fib1(n - 2);
}

function *fib2(n) {
	if (n < 2) return 1;
	return (yield fib2(n - 1)) + (yield fib2(n - 2));
}

function trampoline(it) {
	let stack = [];
	let val;
	while (true) {
		let {done, value} = it.next(val);
		if (done) {
			if (stack.length === 0)
				return value;
			it = stack.pop();
			val = value;
			continue;
		}
		stack.push(it);
		it = value;
		val = undefined;
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
Recursive function
fib1(12);
ready
Generator
trampoline(fib2(12));
ready

Revisions

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