settrace()

Benchmark created on


Setup

const _b_ = {
	None: {}
}
let _trace_fct = _b_.None

$B = {
	enter_frame: function(frame){
		return _trace_fct },
	trace_return: function(result) { console.log('return', result) },
	trace_exception: function() { console.log('enabled'); }
};

$C = {
	enter_frame: function(frame){
		if( _trace_fct === _b_.None )
		return {
			trace_return: function(_arg){},
			trace_exception: function(){}
		};
	}
};

const obj = {
			trace_return: function(_arg){},
			trace_exception: function(){}
		};
$D = {
	enter_frame: function(frame){
		return obj;
	}
};



function foo_A(a,b) {
	let frame = []
	frame.$f_trace = $B.enter_frame(frame)
	try {
		let result = a+b
		if(frame.$f_trace !== _b_.None){
        	$B.trace_return(result)
      	}
		return result;
	} catch(e) {
		
		if(frame.$f_trace !== _b_.None){
        	frame.$f_trace = $B.trace_exception()
      	}
	}
}
function foo_B(a,b) {
	let frame = []
	const trace = frame.$f_trace = $C.enter_frame(frame)
	try {
		let result = a+b
		trace.trace_return(result)
		return result;
	} catch(e) {
		trace.trace_exception()
	}
}
function foo_D(a,b) {
	let frame = []
	const trace = frame.$f_trace = $D.enter_frame(frame)
	try {
		let result = a+b
		trace.trace_return(result)
		return result;
	} catch(e) {
		trace.trace_exception()
	}
}

function foo_C(a,b) {
	let frame = []
	$B.enter_frame(frame)
	try {
		let result = a+b
		return result;
	} catch(e) {}
}

Test runner

Ready to run.

Testing in
TestOps/sec
Brython
let acc = 0;
for(let i = 0; i < 10000; ++i)
    acc += foo_A(i, i)
    

console.log(acc)
ready
Functions instead of conditions
let acc = 0;
for(let i = 0; i < 10000; ++i)
    acc += foo_B(i, i)
    

console.log(acc)
ready
Mode performance
let acc = 0;
for(let i = 0; i < 10000; ++i)
    acc += foo_C(i, i)
    

console.log(acc)
ready
Functions instead of conditions - precomputed
let acc = 0;
for(let i = 0; i < 10000; ++i)
    acc += foo_D(i, i)
    

console.log(acc)
ready

Revisions

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