settrace() (v2)

Revision 2 of this benchmark created on


Setup

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

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

function foo_A(a,b) {
	let frame = []
	frame.$f_trace = $A.enter_frame(frame)
	try {
		let result = a+b
		if(frame.$f_trace !== _b_.None){
        	$A.trace_return(result)
      	}
		return result;
	} catch(e) {
		
		if(frame.$f_trace !== _b_.None){
        	frame.$f_trace = $A.trace_exception()
      	}
	}
}

/*** B ***/

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

function foo_B(a,b) {
	let frame = []
	const trace = frame.$f_trace = $B.enter_frame(frame)
	try {
		let result = a+b
		trace.trace_return(result)
		return result;
	} catch(e) {
		trace.trace_exception()
	}
}

/*** C ***/

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

/*** D ***/

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

function foo_D(a,b) {
	let frame = []
	const trace = $D.enter_frame(frame)
	try {
		let result = a+b
		trace.trace_return(result)
		return result;
	} catch(e) {
		trace.trace_exception()
	}
}


/*** E ***/

const NO_TRACE = function(_arg){};
NO_TRACE.trace_exception = function(){};

function getNO_TRACE() { return NO_TRACE }

$E = {
	enter_frame: getNO_TRACE
};

function foo_E(a,b) {
	let frame = []
	const trace = $E.enter_frame(frame)
	try {
		let result = a+b
		trace(result)
		return result;
	} catch(e) {
		// Or do something else, we don't care if exceptions are less performant
		trace.trace_exception()
	}
}

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
Function instead of conditions - no objects
let acc = 0;
for(let i = 0; i < 10000; ++i)
    acc += foo_E(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.