py2js (v2)

Revision 2 of this benchmark created on


Setup

let obj = {
	a: 32,
	b: 52
};
let obj2 = {
	a:32,
	b: 52
};

function foo(a, b) {
	return a+b
}

let arr = [obj, obj2];

function jsobj2pyobj(jsobj, _this = null) {
	switch(jsobj) {
      case true:
      case false:
        return jsobj
    }

    if(jsobj === undefined){
        return null
    }else if(jsobj === null){
        return null
    }

    if(Array.isArray(jsobj)){
        return jsobj.map(jsobj2pyobj)
    }else if(typeof jsobj === 'number'){
       return null
    }else if(typeof jsobj == "string"){
        return null
    }else if(typeof jsobj == "function"){
            _this = _this === undefined ? null : _this
        return function(){
            var args = []
            for(var i = 0, len = arguments.length; i < len; i++){
                args.push(pyobj2jsobj(arguments[i]))
            }
            return jsobj2pyobj(jsobj.apply(_this, args))
        }
    }

    if(jsobj.kw) {
        return jsobj
    }
	
	return jsobj
}

let wrapper = jsobj2pyobj(obj);
let array_wrapper = jsobj2pyobj(arr);

let wm = new WeakMap();
wm.set(obj, wrapper);
wm.set(arr, array_wrapper);

function cache(obj) {
	obj = wm.get(obj);
	if(obj !== null)
    	return obj
    return jsobj2pyobj(obj)
}

Test runner

Ready to run.

Testing in
TestOps/sec
no cache
jsobj2pyobj(obj)
ready
in cache
cache(obj)
ready
no in cache
cache(obj2)
ready
array
jsobj2pyobj(arr)
ready
cache array
cache(arr)
ready
fct
jsobj2pyobj(foo)
ready
cache fct
cache(foo)
ready

Revisions

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